public static ByteBuffer decodeImage(String file){ String absolutePath = VkImageLoader.class.getClassLoader().getResource(file).getPath().substring(1); if (!System.getProperty("os.name").contains("Windows")) { // TODO Language/region agnostic value for 'Windows' ? // stbi_load requires a file system path, NOT a classpath resource path absolutePath = File.separator + absolutePath; } IntBuffer x = BufferUtils.createIntBuffer(1); IntBuffer y = BufferUtils.createIntBuffer(1); IntBuffer channels = BufferUtils.createIntBuffer(1); ByteBuffer image = stbi_load(absolutePath, x, y, channels, STBI_rgb_alpha); if (image == null) { System.err.println("Could not decode image file ["+ absolutePath +"]: ["+ STBImage.stbi_failure_reason() +"]"); } return image; }
public static ImageMetaData getImageMetaData(String file){ ByteBuffer imageBuffer; try { imageBuffer = ioResourceToByteBuffer(file, 128 * 128); } catch (IOException e) { throw new RuntimeException(e); } IntBuffer x = BufferUtils.createIntBuffer(1); IntBuffer y = BufferUtils.createIntBuffer(1); IntBuffer channels = BufferUtils.createIntBuffer(1); // Use info to read image metadata without decoding the entire image. if (!stbi_info_from_memory(imageBuffer, x, y, channels)) { throw new RuntimeException("Failed to read image information: " + stbi_failure_reason()); } return new ImageMetaData(x.get(0), y.get(0), channels.get(0)); }
throw new RuntimeException("Failed to read image information: " + stbi_failure_reason()); throw new RuntimeException("Failed to load image: " + stbi_failure_reason());
/** * Should be used to load image data from source. */ @Override public void load() { try { ByteBuffer byteBuffer = IOUtil.resourceToByteBuffer(getPath()); int[] width = {0}; int[] height = {0}; int[] channels = {0}; ByteBuffer imageData = STBImage.stbi_load_from_memory(byteBuffer, width, height, channels, 4); if (imageData != null) { this.width = width[0]; this.height = height[0]; this.channels = ImageChannels.instance(channels[0]); this.imageData = imageData; } else { // if error occurs throw LeguiExceptionTemplate.FAILED_TO_LOAD_IMAGE.create(STBImage.stbi_failure_reason()); } } catch (IOException e) { throw LeguiExceptionTemplate.FAILED_TO_LOAD_IMAGE.create(e, e.getMessage()); } }
throw new SilenceException("Failed to load image: " + stbi_failure_reason());