20
20
import org.helioviewer.viewmodel.viewportimagesize.StaticViewportImageSize;
21
21
import org.helioviewer.viewmodel.viewportimagesize.ViewportImageSize;
23
/** Collection of useful functions for use within the view chain.
24
* Collection of useful functions for use within the view chain.
25
* <p>This class provides many different helpful functions, covering
26
* topics such as scaling and alignment of regions, navigation within
27
* the view chain and loading new images
27
* This class provides many different helpful functions, covering topics such as
28
* scaling and alignment of regions, navigation within the view chain and
29
31
* @author Ludwig Schmidt
30
32
* @author Markus Langenberg
33
35
public final class ViewHelper {
35
/** Expands the aspect ratio of the given region to the given viewport.
37
* <p>When a region is resized, it usually does not fit in the viewport
38
* without distorting it. To prevent unused areas or deformed images,
39
* this function expands the region to fit the viewport.
40
* This might not be possible, if the maximum of the region, given in the
41
* meta data, is reached.
43
* <p>Note, that the region is always expanded, never cropped.
45
* <p>Also note, that if the aspect ration already is equal, the given
48
* @param v Target viewport, which the region should fit in
49
* @param r Source region, which has to be expanded
50
* @param m Meta data of the image, to read maximal region
38
* Expands the aspect ratio of the given region to the given viewport.
41
* When a region is resized, it usually does not fit in the viewport without
42
* distorting it. To prevent unused areas or deformed images, this function
43
* expands the region to fit the viewport. This might not be possible, if
44
* the maximum of the region, given in the meta data, is reached.
47
* Note, that the region is always expanded, never cropped.
50
* Also note, that if the aspect ration already is equal, the given region
54
* Target viewport, which the region should fit in
56
* Source region, which has to be expanded
58
* Meta data of the image, to read maximal region
51
59
* @return Expanded region
52
60
* @see #contractRegionToViewportAspectRatio(Viewport, Region, MetaData)
54
public static Region expandRegionToViewportAspectRatio(Viewport v,
55
Region r, MetaData m) {
57
double viewportRatio = v.getAspectRatio();
59
if(Math.abs(r.getWidth() / r.getHeight() - viewportRatio) > 0.01f) {
62
public static Region expandRegionToViewportAspectRatio(Viewport v,
63
Region r, MetaData m) {
65
double viewportRatio = v.getAspectRatio();
67
if (Math.abs(r.getWidth() / r.getHeight() - viewportRatio) > 0.01f) {
60
68
return StaticRegion.createAdaptedRegion(r.getRectangle()
61
.expandToAspectRatioKeepingCenter(viewportRatio)
62
.moveAndCropToOuterRectangle(m.getPhysicalRectangle()));
69
.expandToAspectRatioKeepingCenter(viewportRatio)
70
.moveAndCropToOuterRectangle(m.getPhysicalRectangle()));
68
/** Contracts the aspect ratio of the given region to the given viewport.
70
* <p>When a region is resized, it usually does not fit in the viewport
71
* without distorting it. To prevent unused areas or deformed images,
72
* this function contracts the region to fit the viewport.
74
* <p>Note, that if the aspect ration already is equal, the given
77
* @param v Target viewport, which the region should fit in
78
* @param r Source region, which has to be contracted
79
* @param m Meta data of the image, to read maximal region
77
* Contracts the aspect ratio of the given region to the given viewport.
80
* When a region is resized, it usually does not fit in the viewport without
81
* distorting it. To prevent unused areas or deformed images, this function
82
* contracts the region to fit the viewport.
85
* Note, that if the aspect ration already is equal, the given region is
89
* Target viewport, which the region should fit in
91
* Source region, which has to be contracted
93
* Meta data of the image, to read maximal region
80
94
* @return Contracted region
81
95
* @see #expandRegionToViewportAspectRatio(Viewport, Region, MetaData)
83
public static Region contractRegionToViewportAspectRatio(Viewport v,
84
Region r, MetaData m) {
86
double viewportRatio = v.getAspectRatio();
88
if(Math.abs(r.getWidth() / r.getHeight() - viewportRatio) > 0.01f) {
97
public static Region contractRegionToViewportAspectRatio(Viewport v,
98
Region r, MetaData m) {
100
double viewportRatio = v.getAspectRatio();
102
if (Math.abs(r.getWidth() / r.getHeight() - viewportRatio) > 0.01f) {
89
103
return StaticRegion.createAdaptedRegion(r.getRectangle()
90
.contractToAspectRatioKeepingCenter(viewportRatio)
91
.moveAndCropToOuterRectangle(m.getPhysicalRectangle()));
104
.contractToAspectRatioKeepingCenter(viewportRatio)
105
.moveAndCropToOuterRectangle(m.getPhysicalRectangle()));
97
/** Returns a View of given interface or class, starting search at given view.
112
* Returns a View of given interface or class, starting search at given
99
* If the given view implements the given interface itself, it returns
100
* that very same view, otherwise it returns a suitable view (for example another
101
* view located deeper within the view chain, that can provide the desired
115
* If the given view implements the given interface itself, it returns that
116
* very same view, otherwise it returns a suitable view (for example another
117
* view located deeper within the view chain, that can provide the desired
102
118
* information, or null, if that is not possible).
104
* @param <T> Subclass of {@link View}
105
* @param v First view to analyze
106
* @param c Class or interface to search for
107
* @return View implementing given class or interface, if available, null otherwise
109
public static <T extends View> T getViewAdapter(View v, Class<T> c) {
110
return v == null ? null : v.getAdapter(c);
121
* Subclass of {@link View}
123
* First view to analyze
125
* Class or interface to search for
126
* @return View implementing given class or interface, if available, null
129
public static <T extends View> T getViewAdapter(View v, Class<T> c) {
130
return v == null ? null : v.getAdapter(c);
113
/** Returns an ImageData object of given class or interface.
115
* <p>The function searches the next {@link SubimageDataView}, fetches its
116
* ImageData object and tests, whether it satisfies the given class or
117
* interface. If so, it returns the ImageData object, otherwise, it returns null
119
* @param <T> Subclass of {@link org.helioviewer.viewmodel.imagedata.ImageData}
120
* @param v First view to analyze
121
* @param c Class or interface to search for
122
* @return ImageData implementing given class or interface, if available, null otherwise
124
@SuppressWarnings("unchecked")
134
* Returns an ImageData object of given class or interface.
137
* The function searches the next {@link SubimageDataView}, fetches its
138
* ImageData object and tests, whether it satisfies the given class or
139
* interface. If so, it returns the ImageData object, otherwise, it returns
144
* {@link org.helioviewer.viewmodel.imagedata.ImageData}
146
* First view to analyze
148
* Class or interface to search for
149
* @return ImageData implementing given class or interface, if available,
152
@SuppressWarnings("unchecked")
125
153
public static <T extends ImageData> T getImageDataAdapter(View v, Class<T> c) {
127
SubimageDataView dataView = getViewAdapter(v, SubimageDataView.class);
129
if (dataView == null) {
132
} else if (dataView.getSubimageData() == null) {
134
} else if (!c.isInstance(dataView.getSubimageData())) {
137
return (T) dataView.getSubimageData();
141
/** Calculates the final size of a given region within the viewport.
143
* <p>The resulting size is smaller or equal to the size of the
144
* viewport. It is equal if and only if the aspect ratio of the region
145
* is equal to the aspect ratio of the viewport. Otherwise, the
146
* image size is cropped to keep the regions aspect ratio and not
149
* @param v viewport, in which the image will be displayed
150
* @param r visible region of the image
151
* @return resulting image size of the region within the viewport
153
public static ViewportImageSize calculateViewportImageSize(Viewport v,
155
if (v == null || r == null) {
159
double screenMeterPerPixel;
160
double screenSubImageWidth;
161
double screenSubImageHeight;
163
// fit region of interest into viewport
164
if ((double) v.getWidth() / (double) v.getHeight() > r.getWidth()
166
screenMeterPerPixel = r.getHeight() / v.getHeight();
167
screenSubImageHeight = v.getHeight();
168
screenSubImageWidth = r.getWidth() / screenMeterPerPixel;
170
screenMeterPerPixel = r.getWidth() / v.getWidth();
171
screenSubImageWidth = v.getWidth();
172
screenSubImageHeight = r.getHeight() / screenMeterPerPixel;
175
return StaticViewportImageSize.createAdaptedViewportImageSize(
176
(int) Math.round(screenSubImageWidth), (int) Math
177
.round(screenSubImageHeight));
180
/** Calculates the final size of a given region within the viewport.
182
* <p>The resulting size is smaller or equal to the size of the
183
* viewport. It is equal if and only if the aspect ratio of the region
184
* is equal to the aspect ratio of the viewport. Otherwise, the
185
* image size is cropped to keep the regions aspect ratio and not
188
* <p>Basically, this function fetches the region and viewport of the
189
* given view and calls {@link #calculateViewportImageSize(Viewport, Region)}.
191
* @param v View containing the image.
192
* @return resulting image size of the region within the viewport
194
public static ViewportImageSize calculateViewportImageSize(View v) {
195
RegionView regionView = ViewHelper.getViewAdapter(v, RegionView.class);
196
ViewportView viewportView = ViewHelper.getViewAdapter(v,
198
if (regionView == null || viewportView == null) {
201
return calculateViewportImageSize(viewportView.getViewport(),
202
regionView.getRegion());
205
/** Converts a given displacement on the screen to image coordinates.
207
* @param screenDisplacement Displacement on the screen to convert
208
* @param r Region of the image currently visible on the screen
209
* @param v ViewportImageSize of the image within the current viewport
210
* @return Displacement in image coordinates
212
public static Vector2dDouble convertScreenToImageDisplacement(
213
Vector2dInt screenDisplacement, Region r, ViewportImageSize v) {
214
return convertScreenToImageDisplacement(screenDisplacement.getX(), screenDisplacement.getY(), r, v);
217
/** Converts a given displacement on the screen to image coordinates.
219
* @param screenDisplacementX X-coordinate of the displacement on the screen to convert
220
* @param screenDisplacementY Y-coordinate of the displacement on the screen to convert
221
* @param r Region of the image currently visible on the screen
222
* @param v ViewportImageSize of the image within the current viewport
223
* @return Displacement in image coordinates
225
public static Vector2dDouble convertScreenToImageDisplacement(
226
int screenDisplacementX, int screenDisplacementY, Region r, ViewportImageSize v) {
227
return new Vector2dDouble(
228
r.getWidth() / ((double) v.getWidth()) * screenDisplacementX,
229
-r.getHeight() / ((double) v.getHeight()) * screenDisplacementY);
232
/** Converts a given displacement on the image to screen coordinates.
234
* @param imageDisplacement Displacement on the image to convert
235
* @param r Region of the image currently visible on the screen
236
* @param v ViewportImageSize of the image within the current viewport
237
* @return Displacement in screen coordinates
239
public static Vector2dInt convertImageToScreenDisplacement(
240
Vector2dDouble imageDisplacement, Region r, ViewportImageSize v) {
241
return convertImageToScreenDisplacement(imageDisplacement.getX(), imageDisplacement.getY(), r, v);
244
/** Converts a given displacement on the image to screen coordinates.
246
* @param imageDisplacementX X-coordinate of the displacement on the image to convert
247
* @param imageDisplacementY Y-coordinate of the displacement on the image to convert
248
* @param r Region of the image currently visible on the screen
249
* @param v ViewportImageSize of the image within the current viewport
250
* @return Displacement in screen coordinates
252
public static Vector2dInt convertImageToScreenDisplacement(
253
double imageDisplacementX, double imageDisplacementY, Region r, ViewportImageSize v) {
254
return new Vector2dInt(
255
(int) Math.round(imageDisplacementX / r.getWidth() * v.getWidth()),
256
(int) Math.round(imageDisplacementY / r.getHeight() * v.getHeight()));
259
/** Ensures, that the given region is within the maximal bounds of the image data.
261
* If that is not the case, moves and/or crops the region to the maximal
262
* area given by the meta data.
264
* @param r Region to move and crop, if necessary
265
* @param m Meta data defining the maximal region
266
* @return Region located inside the maximal region
268
public static Region cropRegionToImage(Region r, MetaData m) {
269
return StaticRegion.createAdaptedRegion(r.getRectangle()
270
.moveAndCropToOuterRectangle(m.getPhysicalRectangle()));
273
/** Ensures, that the given inner region is within the given outer region.
275
* If that is not the case, crops the inner region to the outer region.
277
* @param innerRegion Inner region to crop, if necessary
278
* @param outerRegion Outer region, defining the maximal bounds
279
* @return region located inside the outer region
281
public static Region cropInnerRegionToOuterRegion(Region innerRegion,
282
Region outerRegion) {
283
return StaticRegion.createAdaptedRegion(innerRegion.getRectangle()
284
.cropToOuterRectangle(outerRegion.getRectangle()));
287
/** Calculates the inner viewport to the corresponding inner region.
289
* Given the outer region and the outer viewport image size, this function
290
* calculates the part of the outer viewport image size, that is occupied
291
* by the inner region.
293
* @param innerRegion inner region, whose inner viewport is requested
294
* @param outerRegion outer region, as a reference
295
* @param outerViewportImageSize outer viewport image size, as a reference
296
* @return viewport corresponding to the inner region based on the outer
297
* region and viewport image size
298
* @see #calculateInnerViewportOffset
300
public static Viewport calculateInnerViewport(Region innerRegion,
301
Region outerRegion, ViewportImageSize outerViewportImageSize) {
302
double newWidth = outerViewportImageSize.getWidth()
303
* innerRegion.getWidth() / outerRegion.getWidth();
304
double newHeight = outerViewportImageSize.getHeight()
305
* innerRegion.getHeight() / outerRegion.getHeight();
306
return StaticViewport.createAdaptedViewport((int) Math.round(newWidth),
307
(int) Math.round(newHeight));
310
/** Calculates the offset of the inner viewport relative to the outer viewport
313
* Given the outer region and viewport image size, calculates the offset of
314
* the inner viewport corresponding to the given inner region.
316
* @param innerRegion inner region, whose inner viewport offset is requested
317
* @param outerRegion outer region, as a reference
318
* @param outerViewportImageSize outer viewport image size, as a reference
319
* @return offset of the inner viewport based on the outer
320
* region and viewport image size
321
* @see #calculateInnerViewport
323
public static Vector2dInt calculateInnerViewportOffset(Region innerRegion,
324
Region outerRegion, ViewportImageSize outerViewportImageSize) {
325
return ViewHelper.convertImageToScreenDisplacement(
326
innerRegion.getUpperLeftCorner().subtract(
327
outerRegion.getUpperLeftCorner()), outerRegion,
328
outerViewportImageSize).negateY();
331
/** Converts the internal values for different interpolation modes into java rendering hints.
333
* @param interpolationMode Interpolation mode to convert
334
* @return equivalent java rendering hint
336
public static Object ConvertScaleInterpolationModeToRenderingHint(
337
InterpolationMode interpolationMode) {
338
switch(interpolationMode) {
339
case NEAREST_NEIGHBOR:
340
return RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
342
return RenderingHints.VALUE_INTERPOLATION_BILINEAR;
344
return RenderingHints.VALUE_INTERPOLATION_BICUBIC;
350
/** Loads a new image located at the given URI.
352
* <p>Depending on the file type, a different implementation of the ImageInfoView
353
* is chosen. If there is no implementation available for the given
354
* type, an exception is thrown.
356
* <p>Calls {@link #loadView(URI, boolean)} with the boolean set to true.
358
* @param uri URI representing the location of the image
359
* @return ImageInfoView containing the image
360
* @throws IOException if anything went wrong (e.g. type not supported, image not found, etc.)
362
public static ImageInfoView loadView(URI uri) throws IOException {
363
return loadView(uri, true);
366
/** Loads a new image located at the given URI.
368
* <p>Depending on the file type, a different implementation of the ImageInfoView
369
* is chosen. If there is no implementation available for the given
370
* type, an exception is thrown.
372
* @param uri URI representing the location of the image
373
* @param isMainView Whether the view is used as a main view or not
374
* @return ImageInfoView containing the image
375
* @throws IOException if anything went wrong (e.g. type not supported, image not found, etc.)
377
public static ImageInfoView loadView(URI uri, boolean isMainView) throws IOException {
379
if (uri == null || uri.getScheme() == null || uri.toString() == null)
380
throw new IOException("Invalid URI.");
382
String[] parts = uri.toString().split("\\.");
383
String ending = parts[parts.length - 1];
385
if (ending.equals("jpeg") || ending.equals("jpg")
386
|| ending.equals("JPEG") || ending.equals("JPG")
387
|| ending.equals("png") || ending.equals("PNG")) {
389
return new JHVSimpleImageView(uri);
391
} else if (ending.equals("fits") || ending.equals("FITS")
392
|| ending.equals("fts") || ending.equals("FTS")) {
395
return new JHVFITSView(uri);
396
} catch (Exception e) {
397
throw new IOException(e.getMessage());
402
JP2Image jp2Image = new JP2Image(uri);
404
if(jp2Image.isMultiFrame()) {
405
JHVJPXView jpxView = new JHVJPXView(isMainView);
406
jpxView.setJP2Image(jp2Image);
409
JHVJP2View jp2View = new JHVJP2View(isMainView);
410
jp2View.setJP2Image(jp2Image);
413
} catch (Exception e) {
414
throw new IOException(e.getMessage());
155
SubimageDataView dataView = getViewAdapter(v, SubimageDataView.class);
157
if (dataView == null) {
160
} else if (dataView.getSubimageData() == null) {
162
} else if (!c.isInstance(dataView.getSubimageData())) {
165
return (T) dataView.getSubimageData();
170
* Calculates the final size of a given region within the viewport.
173
* The resulting size is smaller or equal to the size of the viewport. It is
174
* equal if and only if the aspect ratio of the region is equal to the
175
* aspect ratio of the viewport. Otherwise, the image size is cropped to
176
* keep the regions aspect ratio and not deform the image.
179
* viewport, in which the image will be displayed
181
* visible region of the image
182
* @return resulting image size of the region within the viewport
184
public static ViewportImageSize calculateViewportImageSize(Viewport v,
186
if (v == null || r == null) {
190
double screenMeterPerPixel;
191
double screenSubImageWidth;
192
double screenSubImageHeight;
194
// fit region of interest into viewport
195
if ((double) v.getWidth() / (double) v.getHeight() > r.getWidth()
197
screenMeterPerPixel = r.getHeight() / v.getHeight();
198
screenSubImageHeight = v.getHeight();
199
screenSubImageWidth = r.getWidth() / screenMeterPerPixel;
201
screenMeterPerPixel = r.getWidth() / v.getWidth();
202
screenSubImageWidth = v.getWidth();
203
screenSubImageHeight = r.getHeight() / screenMeterPerPixel;
206
return StaticViewportImageSize.createAdaptedViewportImageSize(
207
(int) Math.round(screenSubImageWidth), (int) Math
208
.round(screenSubImageHeight));
212
* Calculates the final size of a given region within the viewport.
215
* The resulting size is smaller or equal to the size of the viewport. It is
216
* equal if and only if the aspect ratio of the region is equal to the
217
* aspect ratio of the viewport. Otherwise, the image size is cropped to
218
* keep the regions aspect ratio and not deform the image.
221
* Basically, this function fetches the region and viewport of the given
222
* view and calls {@link #calculateViewportImageSize(Viewport, Region)}.
225
* View containing the image.
226
* @return resulting image size of the region within the viewport
228
public static ViewportImageSize calculateViewportImageSize(View v) {
229
RegionView regionView = ViewHelper.getViewAdapter(v, RegionView.class);
230
ViewportView viewportView = ViewHelper.getViewAdapter(v,
232
if (regionView == null || viewportView == null) {
235
return calculateViewportImageSize(viewportView.getViewport(),
236
regionView.getRegion());
240
* Converts a given displacement on the screen to image coordinates.
242
* @param screenDisplacement
243
* Displacement on the screen to convert
245
* Region of the image currently visible on the screen
247
* ViewportImageSize of the image within the current viewport
248
* @return Displacement in image coordinates
250
public static Vector2dDouble convertScreenToImageDisplacement(
251
Vector2dInt screenDisplacement, Region r, ViewportImageSize v) {
252
return convertScreenToImageDisplacement(screenDisplacement.getX(),
253
screenDisplacement.getY(), r, v);
257
* Converts a given displacement on the screen to image coordinates.
259
* @param screenDisplacementX
260
* X-coordinate of the displacement on the screen to convert
261
* @param screenDisplacementY
262
* Y-coordinate of the displacement on the screen to convert
264
* Region of the image currently visible on the screen
266
* ViewportImageSize of the image within the current viewport
267
* @return Displacement in image coordinates
269
public static Vector2dDouble convertScreenToImageDisplacement(
270
int screenDisplacementX, int screenDisplacementY, Region r,
271
ViewportImageSize v) {
272
return new Vector2dDouble(r.getWidth() / ((double) v.getWidth())
273
* screenDisplacementX, -r.getHeight()
274
/ ((double) v.getHeight()) * screenDisplacementY);
278
* Converts a given displacement on the image to screen coordinates.
280
* @param imageDisplacement
281
* Displacement on the image to convert
283
* Region of the image currently visible on the screen
285
* ViewportImageSize of the image within the current viewport
286
* @return Displacement in screen coordinates
288
public static Vector2dInt convertImageToScreenDisplacement(
289
Vector2dDouble imageDisplacement, Region r, ViewportImageSize v) {
290
return convertImageToScreenDisplacement(imageDisplacement.getX(),
291
imageDisplacement.getY(), r, v);
295
* Converts a given displacement on the image to screen coordinates.
297
* @param imageDisplacementX
298
* X-coordinate of the displacement on the image to convert
299
* @param imageDisplacementY
300
* Y-coordinate of the displacement on the image to convert
302
* Region of the image currently visible on the screen
304
* ViewportImageSize of the image within the current viewport
305
* @return Displacement in screen coordinates
307
public static Vector2dInt convertImageToScreenDisplacement(
308
double imageDisplacementX, double imageDisplacementY, Region r,
309
ViewportImageSize v) {
310
return new Vector2dInt((int) Math.round(imageDisplacementX
311
/ r.getWidth() * v.getWidth()), (int) Math
312
.round(imageDisplacementY / r.getHeight() * v.getHeight()));
316
* Ensures, that the given region is within the maximal bounds of the image
319
* If that is not the case, moves and/or crops the region to the maximal
320
* area given by the meta data.
323
* Region to move and crop, if necessary
325
* Meta data defining the maximal region
326
* @return Region located inside the maximal region
328
public static Region cropRegionToImage(Region r, MetaData m) {
329
return StaticRegion.createAdaptedRegion(r.getRectangle()
330
.moveAndCropToOuterRectangle(m.getPhysicalRectangle()));
334
* Ensures, that the given inner region is within the given outer region.
336
* If that is not the case, crops the inner region to the outer region.
339
* Inner region to crop, if necessary
341
* Outer region, defining the maximal bounds
342
* @return region located inside the outer region
344
public static Region cropInnerRegionToOuterRegion(Region innerRegion,
345
Region outerRegion) {
346
return StaticRegion.createAdaptedRegion(innerRegion.getRectangle()
347
.cropToOuterRectangle(outerRegion.getRectangle()));
351
* Calculates the inner viewport to the corresponding inner region.
353
* Given the outer region and the outer viewport image size, this function
354
* calculates the part of the outer viewport image size, that is occupied by
358
* inner region, whose inner viewport is requested
360
* outer region, as a reference
361
* @param outerViewportImageSize
362
* outer viewport image size, as a reference
363
* @return viewport corresponding to the inner region based on the outer
364
* region and viewport image size
365
* @see #calculateInnerViewportOffset
367
public static Viewport calculateInnerViewport(Region innerRegion,
368
Region outerRegion, ViewportImageSize outerViewportImageSize) {
369
double newWidth = outerViewportImageSize.getWidth()
370
* innerRegion.getWidth() / outerRegion.getWidth();
371
double newHeight = outerViewportImageSize.getHeight()
372
* innerRegion.getHeight() / outerRegion.getHeight();
373
return StaticViewport.createAdaptedViewport((int) Math.round(newWidth),
374
(int) Math.round(newHeight));
378
* Calculates the offset of the inner viewport relative to the outer
379
* viewport image size.
381
* Given the outer region and viewport image size, calculates the offset of
382
* the inner viewport corresponding to the given inner region.
385
* inner region, whose inner viewport offset is requested
387
* outer region, as a reference
388
* @param outerViewportImageSize
389
* outer viewport image size, as a reference
390
* @return offset of the inner viewport based on the outer region and
391
* viewport image size
392
* @see #calculateInnerViewport
394
public static Vector2dInt calculateInnerViewportOffset(Region innerRegion,
395
Region outerRegion, ViewportImageSize outerViewportImageSize) {
396
return ViewHelper.convertImageToScreenDisplacement(
397
innerRegion.getUpperLeftCorner().subtract(
398
outerRegion.getUpperLeftCorner()), outerRegion,
399
outerViewportImageSize).negateY();
403
* Converts the internal values for different interpolation modes into java
406
* @param interpolationMode
407
* Interpolation mode to convert
408
* @return equivalent java rendering hint
410
public static Object ConvertScaleInterpolationModeToRenderingHint(
411
InterpolationMode interpolationMode) {
412
switch (interpolationMode) {
413
case NEAREST_NEIGHBOR:
414
return RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
416
return RenderingHints.VALUE_INTERPOLATION_BILINEAR;
418
return RenderingHints.VALUE_INTERPOLATION_BICUBIC;
424
* Loads a new image located at the given URI.
427
* Depending on the file type, a different implementation of the
428
* ImageInfoView is chosen. If there is no implementation available for the
429
* given type, an exception is thrown.
432
* Calls {@link #loadView(URI, boolean)} with the boolean set to true.
435
* URI representing the location of the image
436
* @return ImageInfoView containing the image
437
* @throws IOException
438
* if anything went wrong (e.g. type not supported, image not
441
public static ImageInfoView loadView(URI uri) throws IOException {
442
return loadView(uri, true);
446
* Loads a new image located at the given URI.
449
* Depending on the file type, a different implementation of the
450
* ImageInfoView is chosen. If there is no implementation available for the
451
* given type, an exception is thrown.
454
* URI representing the location of the image
456
* Whether the view is used as a main view or not
457
* @return ImageInfoView containing the image
458
* @throws IOException
459
* if anything went wrong (e.g. type not supported, image not
462
public static ImageInfoView loadView(URI uri, boolean isMainView)
464
return loadView(uri, uri, isMainView);
468
* Loads a new image located at the given URI.
471
* Depending on the file type, a different implementation of the
472
* ImageInfoView is chosen. If there is no implementation available for the
473
* given type, an exception is thrown.
476
* URI representing the location of the image
478
* URI from which the whole file can be downloaded
480
* @return ImageInfoView containing the image
481
* @throws IOException
482
* if anything went wrong (e.g. type not supported, image not
485
public static ImageInfoView loadView(URI uri, URI downloadURI)
487
return loadView(uri, downloadURI, true);
491
* Loads a new image located at the given URI.
494
* Depending on the file type, a different implementation of the
495
* ImageInfoView is chosen. If there is no implementation available for the
496
* given type, an exception is thrown.
499
* URI representing the location of the image
501
* URI from which the whole file can be downloaded
503
* Whether the view is used as a main view or not
504
* @return ImageInfoView containing the image
505
* @throws IOException
506
* if anything went wrong (e.g. type not supported, image not
509
public static ImageInfoView loadView(URI uri, URI downloadURI,
510
boolean isMainView) throws IOException {
511
if (uri == null || uri.getScheme() == null || uri.toString() == null)
512
throw new IOException("Invalid URI.");
514
String[] parts = uri.toString().split("\\.");
515
String ending = parts[parts.length - 1];
517
if (ending.equals("jpeg") || ending.equals("jpg")
518
|| ending.equals("JPEG") || ending.equals("JPG")
519
|| ending.equals("png") || ending.equals("PNG")) {
521
return new JHVSimpleImageView(uri);
523
} else if (ending.equals("fits") || ending.equals("FITS")
524
|| ending.equals("fts") || ending.equals("FTS")) {
527
return new JHVFITSView(uri);
528
} catch (Exception e) {
529
throw new IOException(e.getMessage());
534
JP2Image jp2Image = new JP2Image(uri, downloadURI);
536
if (jp2Image.isMultiFrame()) {
537
JHVJPXView jpxView = new JHVJPXView(isMainView);
538
jpxView.setJP2Image(jp2Image);
541
JHVJP2View jp2View = new JHVJP2View(isMainView);
542
jp2View.setJP2Image(jp2Image);
545
} catch (Exception e) {
546
throw new IOException(e.getMessage());