/
IosImageLoader.java
268 lines (217 loc) · 9.12 KB
/
IosImageLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.javafx.iio.ios;
import com.sun.glass.utils.NativeLibLoader;
import com.sun.javafx.iio.common.*;
import com.sun.javafx.iio.ImageFrame;
import com.sun.javafx.iio.ImageMetadata;
import com.sun.javafx.iio.ImageStorage.ImageType;
import com.sun.javafx.iio.common.ImageLoaderImpl;
import com.sun.javafx.iio.common.ImageTools;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;
import java.util.HashMap;
import java.net.URL;
import java.net.MalformedURLException;
/**
* A loader for images on iOS platform.
*/
public class IosImageLoader extends ImageLoaderImpl {
/** These constants must match with those in native */
public static final int GRAY = 0;
public static final int GRAY_ALPHA = 1;
public static final int GRAY_ALPHA_PRE = 2;
public static final int PALETTE = 3;
public static final int PALETTE_ALPHA = 4;
public static final int PALETTE_ALPHA_PRE = 5;
public static final int PALETTE_TRANS = 6;
public static final int RGB = 7;
public static final int RGBA = 8;
public static final int RGBA_PRE = 9;
private static final Map<Integer, ImageType> colorSpaceMapping = new HashMap<Integer, ImageType>();
/** Pointer to the native loader */
private long structPointer;
/** Set by native code */
private int inWidth;
private int inHeight;
private int nImages;
private boolean isDisposed = false;
private int delayTime; // applicable to animated images only
private int loopCount; // applicable to animated images only
/***************************** Native Loader methods ******************************************/
/** Set up static method IDs for calls back to Java. */
private static native void initNativeLoading();
/** Create a loader and buffer data from the InputStream. Report progress if requested. */
private native long loadImage(final InputStream stream, boolean reportProgress) throws IOException;
/** Create a loader for the given URL. Report progress if requested. */
private native long loadImageFromURL(final String url, boolean reportProgress) throws IOException;
/** Set native image size */
private native void resizeImage(long structPointer, int width, int height);
/** Return a buffer with decompressed image data */
private native byte[] getImageBuffer(long structPointer, int imageIndex);
/** Return the number of color components */
private native int getNumberOfComponents(long structPointer);
/** Return image color space model code */
private native int getColorSpaceCode(long structPointer);
/** Return image duration for animated images */
private native int getDelayTime(long structPointer);
/** Destroy a loader. */
private static native void disposeLoader(long structPointer);
/*************************** End of Native Loader methods ***************************************/
static {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
NativeLibLoader.loadLibrary("nativeiio");
return null;
});
colorSpaceMapping.put(GRAY, ImageType.GRAY);
colorSpaceMapping.put(GRAY_ALPHA, ImageType.GRAY_ALPHA);
colorSpaceMapping.put(GRAY_ALPHA_PRE, ImageType.GRAY_ALPHA_PRE);
colorSpaceMapping.put(PALETTE, ImageType.PALETTE);
colorSpaceMapping.put(PALETTE_ALPHA, ImageType.PALETTE_ALPHA);
colorSpaceMapping.put(PALETTE_ALPHA_PRE, ImageType.PALETTE_ALPHA_PRE);
colorSpaceMapping.put(PALETTE_TRANS, ImageType.PALETTE_TRANS);
colorSpaceMapping.put(RGB, ImageType.RGB);
colorSpaceMapping.put(RGBA, ImageType.RGBA);
colorSpaceMapping.put(RGBA_PRE, ImageType.RGBA_PRE);
initNativeLoading();
}
/** Called by the native code when input parameters are known. */
private void setInputParameters(
int width,
int height,
int imageCount,
int loopCount) {
inWidth = width;
inHeight = height;
nImages = imageCount;
this.loopCount = loopCount;
}
private void updateProgress(float progressPercentage) {
updateImageProgress(progressPercentage);
}
private boolean shouldReportProgress() {
return listeners != null && !listeners.isEmpty();
}
private void checkNativePointer() throws IOException {
if (structPointer == 0L) {
throw new IOException("Unable to initialize image native loader!");
}
}
private void retrieveDelayTime() {
if (nImages > 1) {
delayTime = getDelayTime(structPointer);
}
}
public IosImageLoader(final String urlString, final ImageDescriptor desc) throws IOException {
super(desc);
// see if the given URL is valid
try {
final URL url = new URL(urlString);
}
catch (MalformedURLException mue) {
throw new IllegalArgumentException("Image loader: Malformed URL!");
}
try {
structPointer = loadImageFromURL(urlString, shouldReportProgress());
} catch (IOException e) {
dispose();
throw e;
}
checkNativePointer();
retrieveDelayTime();
}
public IosImageLoader(final InputStream inputStream, final ImageDescriptor desc) throws IOException {
super(desc);
if (inputStream == null) {
throw new IllegalArgumentException("Image loader: input stream == null");
}
try {
structPointer = loadImage(inputStream, shouldReportProgress());
} catch (IOException e) {
dispose();
throw e;
}
checkNativePointer();
retrieveDelayTime();
}
/**
* @inheritDoc
*/
public synchronized void dispose() {
if (!isDisposed && structPointer != 0L) {
isDisposed = true;
IosImageLoader.disposeLoader(structPointer);
structPointer = 0L;
}
}
protected void finalize() {
dispose();
}
/**
* @inheritDoc
*/
public ImageFrame load(int imageIndex, int width, int height, boolean preserveAspectRatio, boolean smooth)
throws IOException {
if (imageIndex >= nImages) {
dispose();
return null;
}
// Determine output image dimensions.
int[] widthHeight = ImageTools.computeDimensions(inWidth, inHeight, width, height, preserveAspectRatio);
width = widthHeight[0];
height = widthHeight[1];
final ImageMetadata md = new ImageMetadata(
null, // gamma
true, // whether smaller values represent darker shades
null, // a palette index to use as background
null, // background color
null, // a palette index to be used as transparency
delayTime == 0 ? null : delayTime, // the amount of time to pause at the current image (milliseconds).
nImages > 1 ? loopCount : null, // number of loops
width, // image width
height, // image height
null, // image left offset
null, // image top offset
null); // disposal method
updateImageMetadata(md);
resizeImage(structPointer, width, height);
// the color model and the number of components can change when resizing
final int nComponents = getNumberOfComponents(structPointer);
final int colorSpaceCode = getColorSpaceCode(structPointer);
final ImageType imageType = colorSpaceMapping.get(colorSpaceCode);
final byte[] pixels = getImageBuffer(structPointer, imageIndex);
return new ImageFrame(imageType,
ByteBuffer.wrap(pixels),
width,
height,
width * nComponents,
null,
md);
}
}