Skip to content

Commit edca23e

Browse files
committed
Prepare the implementation of dynamic image file-formats
Required for - #1638
1 parent 857ac0b commit edca23e

File tree

10 files changed

+102
-83
lines changed

10 files changed

+102
-83
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/ImageLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void reset() {
151151
public ImageData[] load(InputStream stream) {
152152
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
153153
reset();
154-
data = FileFormat.load(stream, this);
154+
data = FileFormat.load(stream, this, FileFormat.STATIC_ZOOM_FACTOR);
155155
return data;
156156
}
157157

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/FileFormat.java

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,47 @@ public abstract class FileFormat {
5454
} catch (NoClassDefFoundError e) { } // ignore format
5555
}
5656

57+
public static final int STATIC_ZOOM_FACTOR = 100;
58+
59+
static abstract class StaticFileFormat extends FileFormat {
60+
61+
abstract ImageData[] loadFromByteStream();
62+
63+
@Override
64+
ImageData[] loadFromByteStream(int zoom) {
65+
if (zoom == STATIC_ZOOM_FACTOR) {
66+
return loadFromByteStream();
67+
}
68+
return Arrays.stream(loadFromByteStream()) //
69+
.map(d -> {
70+
int width = (int) Math.round(d.width * zoom / 100.);
71+
int height = (int) Math.round(d.height * zoom / 100.);
72+
return d.scaledTo(width, height);
73+
}).toArray(ImageData[]::new);
74+
}
75+
}
76+
5777
LEDataInputStream inputStream;
5878
LEDataOutputStream outputStream;
5979
ImageLoader loader;
6080
int compression;
6181

62-
/**
63-
* Return whether or not the specified input stream
64-
* represents a supported file format.
65-
*/
66-
abstract boolean isFileFormat(LEDataInputStream stream);
82+
/**
83+
* Return whether or not the specified input stream represents a supported file
84+
* format.
85+
*/
86+
abstract boolean isFileFormat(LEDataInputStream stream) throws IOException;
6787

68-
abstract ImageData[] loadFromByteStream();
88+
abstract ImageData[] loadFromByteStream(int zoom);
6989

7090
/**
7191
* Read the specified input stream, and return the
7292
* device independent image array represented by the stream.
7393
*/
74-
public ImageData[] loadFromStream(LEDataInputStream stream) {
94+
public ImageData[] loadFromStream(LEDataInputStream stream, int zoom) {
7595
try {
7696
inputStream = stream;
77-
return loadFromByteStream();
97+
return loadFromByteStream(zoom);
7898
} catch (Exception e) {
7999
if (e instanceof IOException) {
80100
SWT.error(SWT.ERROR_IO, e);
@@ -86,17 +106,23 @@ public ImageData[] loadFromStream(LEDataInputStream stream) {
86106
}
87107

88108
/**
89-
* Read the specified input stream using the specified loader, and
90-
* return the device independent image array represented by the stream.
109+
* Read the specified input stream using the specified loader, and return the
110+
* device independent image array represented by the stream.
91111
*/
92-
public static ImageData[] load(InputStream is, ImageLoader loader) {
112+
public static ImageData[] load(InputStream is, ImageLoader loader, int zoom) {
93113
LEDataInputStream stream = new LEDataInputStream(is);
94114
FileFormat fileFormat = FORMAT_FACTORIES.stream().skip(1) //
95-
.map(Supplier::get).filter(f -> f.isFileFormat(stream)) //
115+
.map(Supplier::get).filter(f -> {
116+
try {
117+
return f.isFileFormat(stream);
118+
} catch (IOException e) {
119+
return false;
120+
}
121+
}) //
96122
.findFirst().orElse(null);
97-
if (fileFormat == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
123+
if (fileFormat == null) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
98124
fileFormat.loader = loader;
99-
return fileFormat.loadFromStream(stream);
125+
return fileFormat.loadFromStream(stream, zoom);
100126
}
101127

102128
/**

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/GIFFileFormat.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2012 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,11 +14,13 @@
1414
package org.eclipse.swt.internal.image;
1515

1616

17+
import java.io.*;
18+
1719
import org.eclipse.swt.*;
1820
import org.eclipse.swt.graphics.*;
19-
import java.io.*;
21+
import org.eclipse.swt.internal.image.FileFormat.*;
2022

21-
public final class GIFFileFormat extends FileFormat {
23+
public final class GIFFileFormat extends StaticFileFormat {
2224
String signature;
2325
int screenWidth, screenHeight, backgroundPixel, bitsPerPixel, defaultDepth;
2426
int disposalMethod = 0;
@@ -51,15 +53,11 @@ static PaletteData grayRamp(int numGrays) {
5153
}
5254

5355
@Override
54-
boolean isFileFormat(LEDataInputStream stream) {
55-
try {
56-
byte[] signature = new byte[3];
57-
stream.read(signature);
58-
stream.unread(signature);
59-
return signature[0] == 'G' && signature[1] == 'I' && signature[2] == 'F';
60-
} catch (Exception e) {
61-
return false;
62-
}
56+
boolean isFileFormat(LEDataInputStream stream) throws IOException {
57+
byte[] signature = new byte[3];
58+
stream.read(signature);
59+
stream.unread(signature);
60+
return signature[0] == 'G' && signature[1] == 'I' && signature[2] == 'F';
6361
}
6462

6563
/**

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/JPEGFileFormat.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2014 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -18,11 +18,13 @@
1818
package org.eclipse.swt.internal.image;
1919

2020

21+
import java.io.*;
22+
2123
import org.eclipse.swt.*;
2224
import org.eclipse.swt.graphics.*;
23-
import java.io.*;
25+
import org.eclipse.swt.internal.image.FileFormat.*;
2426

25-
public final class JPEGFileFormat extends FileFormat {
27+
public final class JPEGFileFormat extends StaticFileFormat {
2628
int restartInterval;
2729
JPEGFrameHeader frameHeader;
2830
int imageWidth, imageHeight;
@@ -1362,16 +1364,14 @@ void inverseDCT(int[] dataUnit) {
13621364
}
13631365
}
13641366
}
1365-
@Override
1366-
boolean isFileFormat(LEDataInputStream stream) {
1367-
try {
1367+
1368+
@Override
1369+
boolean isFileFormat(LEDataInputStream stream) throws IOException {
13681370
JPEGStartOfImage soi = new JPEGStartOfImage(stream);
13691371
stream.unread(soi.reference);
1370-
return soi.verify(); // we no longer check for appN
1371-
} catch (Exception e) {
1372-
return false;
1372+
return soi.verify(); // we no longer check for appN
13731373
}
1374-
}
1374+
13751375
boolean isZeroInColumn(int[] dataUnit, int col) {
13761376
return dataUnit[col + 8] == 0 && dataUnit[col + 16] == 0
13771377
&& dataUnit[col + 24] == 0 && dataUnit[col + 32] == 0

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/OS2BMPFileFormat.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2008 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,27 +14,26 @@
1414
package org.eclipse.swt.internal.image;
1515

1616

17+
import java.io.*;
18+
1719
import org.eclipse.swt.*;
1820
import org.eclipse.swt.graphics.*;
19-
import java.io.*;
21+
import org.eclipse.swt.internal.image.FileFormat.*;
2022

21-
public final class OS2BMPFileFormat extends FileFormat {
23+
public final class OS2BMPFileFormat extends StaticFileFormat {
2224
static final int BMPFileHeaderSize = 14;
2325
static final int BMPHeaderFixedSize = 12;
2426
int width, height, bitCount;
2527

26-
@Override
27-
boolean isFileFormat(LEDataInputStream stream) {
28-
try {
28+
@Override
29+
boolean isFileFormat(LEDataInputStream stream) throws IOException {
2930
byte[] header = new byte[18];
3031
stream.read(header);
3132
stream.unread(header);
3233
int infoHeaderSize = (header[14] & 0xFF) | ((header[15] & 0xFF) << 8) | ((header[16] & 0xFF) << 16) | ((header[17] & 0xFF) << 24);
3334
return header[0] == 0x42 && header[1] == 0x4D && infoHeaderSize == BMPHeaderFixedSize;
34-
} catch (Exception e) {
35-
return false;
3635
}
37-
}
36+
3837
byte[] loadData(byte[] infoHeader) {
3938
int stride = (width * bitCount + 7) / 8;
4039
stride = (stride + 3) / 4 * 4; // Round up to 4 byte multiple

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/PNGFileFormat.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2011 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -19,8 +19,9 @@
1919

2020
import org.eclipse.swt.*;
2121
import org.eclipse.swt.graphics.*;
22+
import org.eclipse.swt.internal.image.FileFormat.*;
2223

23-
public final class PNGFileFormat extends FileFormat {
24+
public final class PNGFileFormat extends StaticFileFormat {
2425
static final int SIGNATURE_LENGTH = 8;
2526
static final int PRIME = 65521;
2627
PngIhdrChunk headerChunk;
@@ -151,9 +152,9 @@ void unloadIntoByteStream(ImageLoader loader) {
151152
PngEncoder encoder = new PngEncoder(loader);
152153
encoder.encode(outputStream);
153154
}
154-
@Override
155-
boolean isFileFormat(LEDataInputStream stream) {
156-
try {
155+
156+
@Override
157+
boolean isFileFormat(LEDataInputStream stream) throws IOException {
157158
byte[] signature = new byte[SIGNATURE_LENGTH];
158159
stream.read(signature);
159160
stream.unread(signature);
@@ -166,10 +167,8 @@ boolean isFileFormat(LEDataInputStream stream) {
166167
if ((signature[6] & 0xFF) != 26) return false; //<CTRL/Z>
167168
if ((signature[7] & 0xFF) != 10) return false; //<LINEFEED>
168169
return true;
169-
} catch (Exception e) {
170-
return false;
171170
}
172-
}
171+
173172
/**
174173
* SWT does not support 16-bit depths. If this image uses
175174
* 16-bit depths, convert the data to an 8-bit depth.

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/TIFFFileFormat.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2009 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,19 +14,20 @@
1414
package org.eclipse.swt.internal.image;
1515

1616

17+
import java.io.*;
18+
1719
import org.eclipse.swt.*;
1820
import org.eclipse.swt.graphics.*;
19-
import java.io.*;
21+
import org.eclipse.swt.internal.image.FileFormat.*;
2022

2123
/**
2224
* Baseline TIFF decoder revision 6.0
2325
* Extension T4-encoding CCITT T.4 1D
2426
*/
25-
public final class TIFFFileFormat extends FileFormat {
27+
public final class TIFFFileFormat extends StaticFileFormat {
2628

27-
@Override
28-
boolean isFileFormat(LEDataInputStream stream) {
29-
try {
29+
@Override
30+
boolean isFileFormat(LEDataInputStream stream) throws IOException {
3031
byte[] header = new byte[4];
3132
stream.read(header);
3233
stream.unread(header);
@@ -36,10 +37,7 @@ boolean isFileFormat(LEDataInputStream stream) {
3637
return false;
3738
}
3839
return true;
39-
} catch (Exception e) {
40-
return false;
4140
}
42-
}
4341

4442
@Override
4543
ImageData[] loadFromByteStream() {

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/image/WinBMPFileFormat.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2011 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,11 +14,13 @@
1414
package org.eclipse.swt.internal.image;
1515

1616

17+
import java.io.*;
18+
1719
import org.eclipse.swt.*;
1820
import org.eclipse.swt.graphics.*;
19-
import java.io.*;
21+
import org.eclipse.swt.internal.image.FileFormat.*;
2022

21-
public final class WinBMPFileFormat extends FileFormat {
23+
public final class WinBMPFileFormat extends StaticFileFormat {
2224
static final int BMPFileHeaderSize = 14;
2325
static final int BMPHeaderFixedSize = 40;
2426

@@ -415,18 +417,16 @@ int decompressRLE8Data(byte[] src, int numBytes, int stride, byte[] dest, int de
415417
}
416418
return 1;
417419
}
418-
@Override
419-
boolean isFileFormat(LEDataInputStream stream) {
420-
try {
420+
421+
@Override
422+
boolean isFileFormat(LEDataInputStream stream) throws IOException {
421423
byte[] header = new byte[18];
422424
stream.read(header);
423425
stream.unread(header);
424426
int infoHeaderSize = (header[14] & 0xFF) | ((header[15] & 0xFF) << 8) | ((header[16] & 0xFF) << 16) | ((header[17] & 0xFF) << 24);
425427
return header[0] == 0x42 && header[1] == 0x4D && infoHeaderSize >= BMPHeaderFixedSize;
426-
} catch (Exception e) {
427-
return false;
428428
}
429-
}
429+
430430
boolean isPaletteBMP(PaletteData pal, int depth) {
431431
switch(depth) {
432432
case 32:

0 commit comments

Comments
 (0)