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
//! PNG decode/encode and load functions, console log macros,
//! argument parsing from javascript input to inner-crate rust types
//! and other utility functions.

use crate::blending::params::{BlendAlgorithmParams, Value};
use crate::blending::BlendAlgorithm;
use crate::errors::PConvertError;
use crate::utils::{decode_png, encode_png};
use crate::utils::{image_compression_from, image_filter_from};
use crate::wasm::conversions::JSONParams;
use image::codecs::png::{CompressionType, FilterType};
use image::{ImageBuffer, Rgba};
use js_sys::{Array, Uint8Array};
use serde_json::Value as JSONValue;
use std::collections::HashMap;
use std::str::FromStr;
use wasm_bindgen::prelude::*;
use wasm_bindgen::Clamped;
use wasm_bindgen_futures::JsFuture;
use web_sys::{File, ImageData};

#[wasm_bindgen]
extern "C" {
    #[derive(Clone, Debug)]
    pub type NodeFs;

    #[wasm_bindgen(js_namespace = console)]
    pub fn log(s: &str);

    #[wasm_bindgen(js_name = require)]
    pub fn node_require(s: &str) -> NodeFs;

    #[wasm_bindgen(method, js_name = readFileSync, structural)]
    fn readFileSync(fs: &NodeFs, path: &str) -> Vec<u8>;

    #[wasm_bindgen(method, js_name = readFile, structural)]
    fn readFile(fs: &NodeFs, path: &str, callback: js_sys::Function);

    #[wasm_bindgen(method, js_name = writeFileSync, structural)]
    fn writeFileSync(fs: &NodeFs, path: &str, data: &[u8]);
}

macro_rules! console_log {
    ($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
}

/// Receives a `File` and returns the decoded PNG byte buffer.
pub async fn load_png(
    file: File,
    demultiply: bool,
) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>, JsValue> {
    let array_buffer = JsFuture::from(file.array_buffer()).await?;
    let uint8_array = Uint8Array::new(&array_buffer);
    let png = decode_png(&uint8_array.to_vec()[..], demultiply)?;
    Ok(png)
}

/// Receives png buffer data and encodes it as a `File` with specified
/// `CompressionType` and `FilterType`.
pub fn encode_file(
    image_buffer: ImageBuffer<Rgba<u8>, Vec<u8>>,
    compression: CompressionType,
    filter: FilterType,
    target_file_name: String,
) -> Result<File, JsValue> {
    let mut encoded_data = Vec::<u8>::with_capacity(image_buffer.to_vec().capacity());
    encode_png(&mut encoded_data, &image_buffer, compression, filter)?;

    unsafe {
        let array_buffer = Uint8Array::view(&encoded_data);
        File::new_with_u8_array_sequence(&Array::of1(&array_buffer), &target_file_name)
    }
}

/// Receives png buffer data and encodes it as an `ImageData` object with
/// specified `CompressionType` and `FilterType`.
pub fn encode_image_data(
    image_buffer: ImageBuffer<Rgba<u8>, Vec<u8>>,
    compression: CompressionType,
    filter: FilterType,
) -> Result<ImageData, JsValue> {
    let (width, height) = image_buffer.dimensions();

    let mut encoded_data = Vec::<u8>::with_capacity(image_buffer.to_vec().capacity());
    encode_png(&mut encoded_data, &image_buffer, compression, filter)?;

    let bytes = &mut image_buffer.to_vec();
    let clamped_bytes: Clamped<&mut [u8]> = Clamped(bytes);

    ImageData::new_with_u8_clamped_array_and_sh(clamped_bytes, width, height)
}

/// Attempts to parse a `&String` to a `BlendAlgorithm`.
/// Returns the enum variant if it suceeds. Otherwise it returns a `PConvertError`.
pub fn build_algorithm(algorithm: &String) -> Result<BlendAlgorithm, PConvertError> {
    match BlendAlgorithm::from_str(&algorithm) {
        Ok(algorithm) => Ok(algorithm),
        Err(algorithm) => Err(PConvertError::ArgumentError(format!(
            "Invalid algorithm '{}'",
            algorithm
        ))),
    }
}

/// Attempts to build a vector of blending operations and extra parameters.
/// One pair per blending operation. Returns a `PConvertError` if it fails parsing.
pub fn build_params(
    algorithms: Box<[JsValue]>,
) -> Result<Vec<(BlendAlgorithm, Option<BlendAlgorithmParams>)>, PConvertError> {
    let mut result = Vec::new();

    for i in 0..algorithms.len() {
        let element = &algorithms[i];
        if element.is_string() {
            let algorithm =
                build_algorithm(&element.as_string().unwrap_or("multiplicative".to_string()))?;

            result.push((algorithm, None));
        } else if element.is_object() {
            let params: JSONParams = element.into_serde::<JSONParams>().unwrap();
            let algorithm = build_algorithm(&params.algorithm)?;

            let mut blending_params = BlendAlgorithmParams::new();
            for (param_name, param_value) in params.params {
                let param_value: Value = param_value.into();
                blending_params.insert(param_name, param_value);
            }

            result.push((algorithm, Some(blending_params)));
        }
    }

    Ok(result)
}

/// Retrieves the `image::codecs::png::CompressionType` value from the
/// `HashMap<String, JSONValue>` map if it exists.
/// Otherwise it returns the default value: `CompressionType::Fast`.
pub fn get_compression_type(options: &Option<HashMap<String, JSONValue>>) -> CompressionType {
    options.as_ref().map_or(CompressionType::Fast, |options| {
        options
            .get("compression")
            .map_or(CompressionType::Fast, |compression| match compression {
                JSONValue::String(compression) => image_compression_from(compression.to_string()),
                _ => CompressionType::Fast,
            })
    })
}

/// Retrieves the `image::codecs::png::FilterType` value from the
/// `HashMap<String, JSONValue>` map if it exists.
/// Otherwise it returns the default value: `FilterType::NoFilter`.
pub fn get_filter_type(options: &Option<HashMap<String, JSONValue>>) -> FilterType {
    options.as_ref().map_or(FilterType::NoFilter, |options| {
        options
            .get("filter")
            .map_or(FilterType::NoFilter, |filter| match filter {
                JSONValue::String(filter) => image_filter_from(filter.to_string()),
                _ => FilterType::NoFilter,
            })
    })
}

/// Logs the header/column names of the benchmarks table to the browser
/// console (with `console.log`).
pub fn log_benchmark_header() {
    console_log!(
        "{:<20}{:<20}{:<20}{:<20}",
        "Algorithm",
        "Compression",
        "Filter",
        "Times"
    );
}

/// Logs one line (algorithm, compression, filter, blend time, read time, write time)
/// of the benchmarks table to the browser console (with `console.log`).
pub fn log_benchmark(
    algorithm: String,
    compression: CompressionType,
    filter: FilterType,
    blend_time: f64,
    read_time: f64,
    write_time: f64,
) {
    console_log!(
        "{:<20}{:<20}{:<20}{:<20}",
        algorithm,
        format!("{:#?}", compression),
        format!("{:#?}", filter),
        format!(
            "{}ms (blend {}ms, read {}ms, write {}ms)",
            read_time + blend_time + write_time,
            blend_time,
            read_time,
            write_time
        )
    );
}

/// Wrapper function for nodejs `fs.readFileSync`.
pub fn node_read_file_sync(fs: &NodeFs, path: &str) -> Vec<u8> {
    fs.readFileSync(path)
}

/// Rust Future from nodejs `fs.readFile` Promise (awaitable in node).
pub fn node_read_file_async(fs: &NodeFs, path: &str) -> wasm_bindgen_futures::JsFuture {
    let promise = js_sys::Promise::new(&mut |resolve, reject| {
        let callback = js_sys::Function::new_with_args(
            "resolve, reject, err, data",
            "err ? reject(err) : resolve(data);",
        )
        .bind2(&JsValue::NULL, &resolve, &reject);
        fs.readFile(path, callback)
    });

    wasm_bindgen_futures::JsFuture::from(promise)
}

/// Wrapper function for nodejs `fs.writeFileSync`.
pub fn node_write_file_sync(fs: &NodeFs, path: &str, data: &[u8]) {
    fs.writeFileSync(path, data);
}