# resource/imageasset (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/resource/imageasset/

Image asset. (the original resource/imageasset.js)
- The original sets src on an HTMLImageElement and waits for onload.
  The Rust version asks the engine for the bytes, and once they arrive decodes them into an Image on the first get_image. (formats of Image::load_from_memory, on web it uses the image the browser decoded on receipt from the handle)
  For ones received by AssetLoader, the image already decoded when everything arrived (the handle's get_decoded_image) is used as is. (if it could not be decoded, the handle fails)
- load, load_from_bytes return a load handle that can be awaited as in the original. Like the original img.onload, await finishes only once decoding finishes,
  and if it cannot be decoded it finishes in failure (the reject of the original onerror). The decoded image is kept on the handle so get_image does not decode it again.

## ImageAsset (struct) (Default, Asset)

Image asset.
- Once the bytes arrive, decodes them on the first get_image and holds an Image. (formats of Image::load_from_memory such as PNG, JPEG)

Example: Read a PNG from the repository and check its size

```rust
use vanilla::{Engine, EngineConfiguration, ImageAsset, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut image_asset = ImageAsset::new();
let load_handle = image_asset.load(&engine, "examples/assets/atlas/icons.png");
assert!(load_handle.is_loaded());
let image = image_asset.get_image().expect("decoded image");
assert_eq!(image.get_width(), 128);
assert_eq!(image_asset.get_image_size(), Vector2::create(128.0, 128.0));
```

- `pub fn new() -> ImageAsset` (constructor, since 0.3.2): Constructor.
  - returns ImageAsset: Empty image asset before a request.
- `pub fn load(&mut self, engine: &Engine, asset_path: &str) -> AssetLoadHandle` (method, since 0.3.2): Requests a read. (the original load, returns a load handle that can be awaited. Finishes only once decoding finishes)
  - If already requested, does not request again and returns the first handle.
  - `engine` &Engine: Engine to ask for the bytes.
  - `asset_path` &str: Asset path. (relative to the asset folder on desktop, to asset_base_url on the web; data: URLs also work)
  - returns AssetLoadHandle: Load handle. (on desktop the bytes are read on the spot. Finishes in failure if it cannot be read)
- `pub fn load_from_bytes(&mut self, asset_path: &str, bytes: Vec<u8>) -> AssetLoadHandle` (method, since 0.3.2): Reads from bytes already in hand. (the original loadFromBytes, returns a load handle that can be awaited. Finishes in failure if it cannot be decoded)
  - Discards the previously decoded image and decodes again from these bytes.
  - `asset_path` &str: Path to record. (used in notice text)
  - `bytes` Vec<u8>: Image file bytes.
  - returns AssetLoadHandle: Load handle whose bytes are fully read.

  Example: Read from file bytes, and broken bytes

  ```rust
  use vanilla::ImageAsset;
  
  let png_bytes = std::fs::read("examples/assets/atlas/icons.png").expect("png file");
  let mut image_asset = ImageAsset::new();
  image_asset.load_from_bytes("icons.png", png_bytes);
  assert_eq!(image_asset.get_image_size().x, 128.0);
  image_asset.load_from_bytes("broken.png", vec![0, 1, 2, 3]);
  assert!(image_asset.get_image().is_none());
  assert_eq!(image_asset.get_image_size().x, 0.0);
  ```

- `pub fn unload(&mut self)` (method, since 0.3.2): Unloads.
- `pub fn get_image(&mut self) -> Option<Rc<Image>>` (method, since 0.3.2): Image. (None before it is fully read or if decoding fails)
  - Decodes once on the first call and returns the decoded one after that. If it cannot be decoded, prints the reason to standard error and does not decode again.
  - returns Option<Rc<Image>>: Decoded image. None before a request, while reading, or if reading or decoding failed.
- `pub fn get_image_size(&mut self) -> Vector2` (method, since 0.3.2): Image size. (pixels, 0 if not yet)
  - returns Vector2: Width and height of the image. (pixels) (0, 0) if there is no image.
