Skip to content

Commit a123010

Browse files
HeikoKlareHannesWellMichael5601
committed
Improve signature of methods in InternalImageLoader
The InternalImageLoader is currently placed in the graphics package and marked as package internal. This change moves it to the internal package improves its name to NativeImageLoader. It also adapts its method's signatures to properly combine element and zoom information if appropriate. Co-authored-by: Hannes Wellmann <[email protected]> Co-authored-by: Michael Bangas <[email protected]>
1 parent fbdacfb commit a123010

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/InternalImageLoader.java renamed to bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/internal/NativeImageLoader.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
* Contributors:
1212
* Hannes Wellmann - initial API and implementation
1313
*******************************************************************************/
14-
package org.eclipse.swt.graphics;
14+
package org.eclipse.swt.internal;
1515

1616
import java.io.*;
1717
import java.util.*;
1818

19+
import org.eclipse.swt.graphics.*;
1920
import org.eclipse.swt.internal.DPIUtil.*;
2021
import org.eclipse.swt.internal.image.*;
2122

22-
class InternalImageLoader {
23+
public class NativeImageLoader {
2324

24-
static List<ElementAtZoom<ImageData>> load(InputStream stream, ImageLoader imageLoader, int fileZoom, int targetZoom) {
25-
return FileFormat.load(stream, imageLoader, fileZoom, targetZoom);
25+
public static List<ElementAtZoom<ImageData>> load(ElementAtZoom<InputStream> streamAtZoom, ImageLoader imageLoader, int targetZoom) {
26+
return FileFormat.load(streamAtZoom, imageLoader, targetZoom);
2627
}
2728

28-
static void save(OutputStream stream, int format, ImageLoader imageLoader) {
29+
public static void save(OutputStream stream, int format, ImageLoader imageLoader) {
2930
FileFormat.save(stream, format, imageLoader);
3031
}
3132
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.*;
1919

2020
import org.eclipse.swt.*;
21+
import org.eclipse.swt.internal.*;
2122
import org.eclipse.swt.internal.DPIUtil.*;
2223
import org.eclipse.swt.internal.image.*;
2324

@@ -157,7 +158,7 @@ public ImageData[] load(InputStream stream) {
157158
List<ElementAtZoom<ImageData>> load(InputStream stream, int fileZoom, int targetZoom) {
158159
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
159160
reset();
160-
List<ElementAtZoom<ImageData>> images = InternalImageLoader.load(stream, this, fileZoom, targetZoom);
161+
List<ElementAtZoom<ImageData>> images = NativeImageLoader.load(new ElementAtZoom<>(stream, fileZoom), this, targetZoom);
161162
data = images.stream().map(ElementAtZoom::element).toArray(ImageData[]::new);
162163
return images;
163164
}
@@ -229,7 +230,7 @@ List<ElementAtZoom<ImageData>> load(String filename, int fileZoom, int targetZoo
229230
*/
230231
public void save(OutputStream stream, int format) {
231232
if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
232-
InternalImageLoader.save(stream, format, this);
233+
NativeImageLoader.save(stream, format, this);
233234
}
234235

235236
/**

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

+8
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,14 @@ public static int mapZoomToDPI (int zoom) {
521521
* @param <T> type of the element to be presented, e.g., {@link ImageData}
522522
*/
523523
public record ElementAtZoom<T>(T element, int zoom) {
524+
public ElementAtZoom {
525+
if (element == null) {
526+
SWT.error(SWT.ERROR_NULL_ARGUMENT);
527+
}
528+
if (zoom <= 0) {
529+
SWT.error(SWT.ERROR_INVALID_ARGUMENT);
530+
}
531+
}
524532
}
525533

526534
/**

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ public List<ElementAtZoom<ImageData>> loadFromStream(LEDataInputStream stream, i
126126
* Read the specified input stream using the specified loader, and
127127
* return the device independent image array represented by the stream.
128128
*/
129-
public static List<ElementAtZoom<ImageData>> load(InputStream is, ImageLoader loader, int fileZoom, int targetZoom) {
130-
LEDataInputStream stream = new LEDataInputStream(is);
129+
public static List<ElementAtZoom<ImageData>> load(ElementAtZoom<InputStream> is, ImageLoader loader, int targetZoom) {
130+
LEDataInputStream stream = new LEDataInputStream(is.element());
131131
FileFormat fileFormat = determineFileFormat(stream).orElseGet(() -> {
132132
SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
133133
return null;
134134
});
135135
fileFormat.loader = loader;
136-
return fileFormat.loadFromStream(stream, fileZoom, targetZoom);
136+
return fileFormat.loadFromStream(stream, is.zoom(), targetZoom);
137137
}
138138

139139
/**

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/InternalImageLoader.java renamed to bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/internal/NativeImageLoader.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,30 @@
1313
* Red Hat - initial implementation
1414
* Hannes Wellmann - Unify ImageLoader implementations and extract differences into InternalImageLoader
1515
*******************************************************************************/
16-
package org.eclipse.swt.graphics;
16+
package org.eclipse.swt.internal;
1717

1818
import java.io.*;
1919
import java.util.*;
2020
import java.util.List;
2121

2222
import org.eclipse.swt.*;
23-
import org.eclipse.swt.internal.*;
23+
import org.eclipse.swt.graphics.*;
2424
import org.eclipse.swt.internal.DPIUtil.*;
2525
import org.eclipse.swt.internal.gtk.*;
2626
import org.eclipse.swt.internal.image.*;
2727
import org.eclipse.swt.widgets.*;
2828

29-
class InternalImageLoader {
29+
public class NativeImageLoader {
3030

3131
/** If the 29th byte of the PNG file is not zero, then it is interlaced. */
3232
private static final int PNG_INTERLACE_METHOD_OFFSET = 28;
3333

3434
// --- loading ---
3535

36-
static List<ElementAtZoom<ImageData>> load(InputStream stream, ImageLoader imageLoader, int fileZoom, int targetZoom) {
36+
public static List<ElementAtZoom<ImageData>> load(ElementAtZoom<InputStream> streamAtZoom, ImageLoader imageLoader, int targetZoom) {
3737
// 1) Load InputStream into byte array
3838
byte[] data_buffer;
39+
InputStream stream = streamAtZoom.element();
3940
try (stream) {
4041
data_buffer = stream.readAllBytes();
4142
} catch (IOException e) {
@@ -52,7 +53,7 @@ static List<ElementAtZoom<ImageData>> load(InputStream stream, ImageLoader image
5253
} catch (IOException e) {
5354
SWT.error(SWT.ERROR_IO);
5455
}
55-
return FileFormat.load(stream2, imageLoader, fileZoom, targetZoom);
56+
return FileFormat.load(new ElementAtZoom<>(stream2, streamAtZoom.zoom()), imageLoader, targetZoom);
5657
}
5758
List<ImageData> imgDataList = new ArrayList<>();
5859
long loader = GDK.gdk_pixbuf_loader_new();
@@ -138,7 +139,7 @@ static List<ElementAtZoom<ImageData>> load(InputStream stream, ImageLoader image
138139
}
139140
OS.g_free(buffer_ptr);
140141
OS.g_object_unref(loader);
141-
return Arrays.stream(imgDataArray).map(data -> new ElementAtZoom<>(data, fileZoom)).toList();
142+
return Arrays.stream(imgDataArray).map(data -> new ElementAtZoom<>(data, streamAtZoom.zoom())).toList();
142143
}
143144

144145
/**
@@ -256,7 +257,7 @@ private static ImageData pixbufToImageData(long pixbuf) {
256257

257258
// --- saving ---
258259

259-
static void save(OutputStream stream, int format, ImageLoader imageLoader) {
260+
public static void save(OutputStream stream, int format, ImageLoader imageLoader) {
260261
if (format == -1) {
261262
SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);
262263
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/InternalImageLoader.java renamed to bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/NativeImageLoader.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111
* Contributors:
1212
* Hannes Wellmann - initial API and implementation
1313
*******************************************************************************/
14-
package org.eclipse.swt.graphics;
14+
package org.eclipse.swt.internal;
1515

1616
import java.io.*;
1717
import java.util.*;
1818

19+
import org.eclipse.swt.graphics.*;
1920
import org.eclipse.swt.internal.DPIUtil.*;
2021
import org.eclipse.swt.internal.image.*;
2122

22-
class InternalImageLoader {
23+
public class NativeImageLoader {
2324

24-
static List<ElementAtZoom<ImageData>> load(InputStream stream, ImageLoader imageLoader, int fileZoom, int targetZoom) {
25-
return FileFormat.load(stream, imageLoader, fileZoom, targetZoom);
25+
public static List<ElementAtZoom<ImageData>> load(ElementAtZoom<InputStream> streamAtZoom, ImageLoader imageLoader, int targetZoom) {
26+
return FileFormat.load(streamAtZoom, imageLoader, targetZoom);
2627
}
2728

28-
static void save(OutputStream stream, int format, ImageLoader imageLoader) {
29+
public static void save(OutputStream stream, int format, ImageLoader imageLoader) {
2930
FileFormat.save(stream, format, imageLoader);
3031
}
3132
}

0 commit comments

Comments
 (0)