207 return result; 208 } 209 } 210 211 CImage(long nsImagePtr) { 212 super(nsImagePtr, true); 213 } 214 215 /** @return A MultiResolution image created from nsImagePtr, or null. */ 216 private BufferedImage toImage() { 217 if (ptr == 0) return null; 218 219 final Dimension2D size = nativeGetNSImageSize(ptr); 220 final int w = (int)size.getWidth(); 221 final int h = (int)size.getHeight(); 222 223 Dimension2D[] sizes 224 = nativeGetNSImageRepresentationSizes(ptr, 225 size.getWidth(), size.getHeight()); 226 227 if (sizes == null || sizes.length < 2) { 228 return toImage(w, h, w, h); 229 } 230 231 BufferedImage[] images = new BufferedImage[sizes.length]; 232 int currentImageIndex = 0; 233 234 for (int i = 0; i < sizes.length; i++) { 235 int imageRepWidth = (int) sizes[i].getWidth(); 236 int imageRepHeight = (int) sizes[i].getHeight(); 237 238 if(imageRepHeight <= w && imageRepHeight <= h){ 239 currentImageIndex = i; 240 } 241 images[i] = toImage(w, h, imageRepWidth, imageRepHeight); 242 } 243 return new MultiResolutionBufferedImage(BufferedImage.TYPE_INT_ARGB_PRE, 244 currentImageIndex, images); 245 } 246 247 private BufferedImage toImage(int srcWidth, int srcHeight, int dstWidth, int dstHeight) { 248 final BufferedImage bimg = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB_PRE); 249 final DataBufferInt dbi = (DataBufferInt)bimg.getRaster().getDataBuffer(); 250 final int[] buffer = SunWritableRaster.stealData(dbi, 0); 251 nativeCopyNSImageIntoArray(ptr, buffer, srcWidth, srcHeight, dstWidth, dstHeight); 252 SunWritableRaster.markDirty(dbi); 253 return bimg; 254 } 255 256 /** If nsImagePtr != 0 then scale this NSImage. @return *this* */ 257 CImage resize(final double w, final double h) { 258 if (ptr != 0) nativeSetNSImageSize(ptr, w, h); 259 return this; 260 } 261 262 void resizeRepresentations(double w, double h) { 263 if (ptr != 0) nativeResizeNSImageRepresentations(ptr, w, h); 264 } | 207 return result; 208 } 209 } 210 211 CImage(long nsImagePtr) { 212 super(nsImagePtr, true); 213 } 214 215 /** @return A MultiResolution image created from nsImagePtr, or null. */ 216 private BufferedImage toImage() { 217 if (ptr == 0) return null; 218 219 final Dimension2D size = nativeGetNSImageSize(ptr); 220 final int w = (int)size.getWidth(); 221 final int h = (int)size.getHeight(); 222 223 Dimension2D[] sizes 224 = nativeGetNSImageRepresentationSizes(ptr, 225 size.getWidth(), size.getHeight()); 226 227 BufferedImage baseImage = toImage(w, h, w, h); 228 229 return sizes == null || sizes.length < 2 ? baseImage 230 : new MultiResolutionBufferedImage(baseImage, sizes, 231 (width, height) -> toImage(w, h, width, height)); 232 } 233 234 private BufferedImage toImage(int srcWidth, int srcHeight, int dstWidth, int dstHeight) { 235 final BufferedImage bimg = new BufferedImage(dstWidth, dstHeight, BufferedImage.TYPE_INT_ARGB_PRE); 236 final DataBufferInt dbi = (DataBufferInt)bimg.getRaster().getDataBuffer(); 237 final int[] buffer = SunWritableRaster.stealData(dbi, 0); 238 nativeCopyNSImageIntoArray(ptr, buffer, srcWidth, srcHeight, dstWidth, dstHeight); 239 SunWritableRaster.markDirty(dbi); 240 return bimg; 241 } 242 243 /** If nsImagePtr != 0 then scale this NSImage. @return *this* */ 244 CImage resize(final double w, final double h) { 245 if (ptr != 0) nativeSetNSImageSize(ptr, w, h); 246 return this; 247 } 248 249 void resizeRepresentations(double w, double h) { 250 if (ptr != 0) nativeResizeNSImageRepresentations(ptr, w, h); 251 } |