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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Web Assembly (WASM) extension, exported functions and type conversions.

#[macro_use]
pub mod utils;

pub mod benchmark;
pub mod conversions;

use crate::blending::params::BlendAlgorithmParams;
use crate::blending::{
    blend_images, demultiply_image, get_blending_algorithm, is_algorithm_multiplied, BlendAlgorithm,
};
use crate::constants;
use crate::errors::PConvertError;
use crate::utils::{decode_png, encode_png};
use image::{ImageBuffer, Rgba, RgbaImage};
use js_sys::try_iter;
use serde_json::json;
use serde_json::Value as JSONValue;
use std::collections::HashMap;
use utils::{
    build_algorithm, build_params, encode_file, encode_image_data, get_compression_type,
    get_filter_type, load_png, node_read_file_async, node_read_file_sync, node_require,
    node_write_file_sync,
};
use wasm_bindgen::prelude::*;
use web_sys::{File, ImageData};

/// Blends two `File`s into one, named `target_file_name`, using `algorithm` and the extra
/// `options` given. Algorithm defaults to `BlendAlgorithm::Multiplicative`.
#[wasm_bindgen(js_name = blendImages)]
pub async fn blend_images_js(
    bot: File,
    top: File,
    target_file_name: String,
    algorithm: Option<String>,
    is_inline: Option<bool>,
    options: JsValue,
) -> Result<File, JsValue> {
    let options = match options.is_object() {
        true => options.into_serde::<HashMap<String, JSONValue>>().ok(),
        false => None,
    };

    let mut bot = load_png(bot, false).await?;
    let mut top = load_png(top, false).await?;

    blend_image_buffers(&mut bot, &mut top, algorithm, is_inline)?;

    encode_file(
        bot,
        get_compression_type(&options),
        get_filter_type(&options),
        target_file_name,
    )
}

/// Blends two `ImageData` objects into one using `algorithm` and the extra
/// `options` given. Algorithm defaults to `BlendAlgorithm::Multiplicative`.
#[wasm_bindgen(js_name = blendImagesData)]
pub fn blend_images_data_js(
    bot: ImageData,
    top: ImageData,
    algorithm: Option<String>,
    is_inline: Option<bool>,
    options: JsValue,
) -> Result<ImageData, JsValue> {
    let options = match options.is_object() {
        true => options.into_serde::<HashMap<String, JSONValue>>().ok(),
        false => None,
    };

    let (width, height) = (bot.width(), bot.height());
    let mut bot = ImageBuffer::from_vec(width, height, bot.data().to_vec()).ok_or(
        PConvertError::ArgumentError("Could not parse \"bot\"".to_string()),
    )?;
    let mut top = ImageBuffer::from_vec(width, height, top.data().to_vec()).ok_or(
        PConvertError::ArgumentError("Could not parse \"top\"".to_string()),
    )?;

    blend_image_buffers(&mut bot, &mut top, algorithm, is_inline)?;

    encode_image_data(
        bot,
        get_compression_type(&options),
        get_filter_type(&options),
    )
}

/// Blends two image buffers using `algorithm` and the extra
/// `options` given. Algorithm defaults to `BlendAlgorithm::Multiplicative`.
pub fn blend_image_buffers(
    bot: &mut ImageBuffer<Rgba<u8>, Vec<u8>>,
    top: &mut ImageBuffer<Rgba<u8>, Vec<u8>>,
    algorithm: Option<String>,
    is_inline: Option<bool>,
) -> Result<(), PConvertError> {
    let algorithm = algorithm.unwrap_or(String::from("multiplicative"));
    let algorithm = build_algorithm(&algorithm)?;
    let algorithm_fn = get_blending_algorithm(&algorithm);
    let demultiply = is_algorithm_multiplied(&algorithm);
    let _is_inline = is_inline.unwrap_or(false);

    if demultiply {
        demultiply_image(bot);
        demultiply_image(top);
    }

    blend_images(bot, &top, &algorithm_fn, &None);
    Ok(())
}

/// Blends multiple `File`s into one, named `target_file_name`, using `algorithm` and the extra
/// `options` given. Algorithm defaults to `BlendAlgorithm::Multiplicative`.
#[wasm_bindgen(js_name = blendMultiple)]
pub async fn blend_multiple_js(
    image_files: JsValue,
    target_file_name: String,
    algorithm: Option<String>,
    algorithms: Option<Box<[JsValue]>>,
    is_inline: Option<bool>,
    options: JsValue,
) -> Result<File, JsValue> {
    let options = match options.is_object() {
        true => options.into_serde::<HashMap<String, JSONValue>>().ok(),
        false => None,
    };

    let mut image_buffers = Vec::new();
    let image_files = try_iter(&image_files).unwrap().unwrap();
    for file in image_files {
        let file = file?;
        let img = load_png(file.into(), false).await?;

        image_buffers.push(img);
    }

    let composition = blend_multiple_buffers(image_buffers, algorithm, algorithms, is_inline)?;
    encode_file(
        composition,
        get_compression_type(&options),
        get_filter_type(&options),
        target_file_name,
    )
}

/// Blends multiple `ImageData` objects into one using `algorithm` and the extra
/// `options` given. Algorithm defaults to `BlendAlgorithm::Multiplicative`.
#[wasm_bindgen(js_name = blendMultipleData)]
pub fn blend_multiple_data_js(
    images: &JsValue,
    algorithm: Option<String>,
    algorithms: Option<Box<[JsValue]>>,
    is_inline: Option<bool>,
    options: JsValue,
) -> Result<ImageData, JsValue> {
    let options = match options.is_object() {
        true => options.into_serde::<HashMap<String, JSONValue>>().ok(),
        false => None,
    };

    let mut image_buffers: Vec<RgbaImage> = Vec::new();
    let mut images = try_iter(images).unwrap().unwrap();
    while let Some(Ok(img_data)) = images.next() {
        let img_data: ImageData = img_data.into();
        let img_buffer: RgbaImage = ImageBuffer::from_vec(
            img_data.width(),
            img_data.height(),
            img_data.data().to_vec(),
        )
        .ok_or(PConvertError::ArgumentError(
            "Could not parse \"bot\"".to_string(),
        ))?;

        image_buffers.push(img_buffer);
    }

    let composition = blend_multiple_buffers(image_buffers, algorithm, algorithms, is_inline)?;
    encode_image_data(
        composition,
        get_compression_type(&options),
        get_filter_type(&options),
    )
}

/// Returns a JSON object with the module constants (e.g. ALGORITHMS, COMPILER, COMPILER_VERSION, ...).
#[wasm_bindgen(js_name = getModuleConstants)]
pub fn get_module_constants_js() -> JsValue {
    let filters: Vec<String> = constants::FILTER_TYPES
        .to_vec()
        .iter()
        .map(|x| format!("{:?}", x))
        .collect();

    let compressions: Vec<String> = constants::COMPRESSION_TYPES
        .to_vec()
        .iter()
        .map(|x| format!("{:?}", x))
        .collect();

    JsValue::from_serde(&json!({
        "COMPILATION_DATE": constants::COMPILATION_DATE,
        "COMPILATION_TIME": constants::COMPILATION_TIME,
        "VERSION": constants::VERSION,
        "ALGORITHMS": constants::ALGORITHMS,
        "COMPILER": constants::COMPILER,
        "COMPILER_VERSION": constants::COMPILER_VERSION,
        "LIBPNG_VERSION": constants::LIBPNG_VERSION,
        "FEATURES": constants::FEATURES,
        "PLATFORM_CPU_BITS": constants::PLATFORM_CPU_BITS,
        "FILTER_TYPES": filters,
        "COMPRESSION_TYPES": compressions
    }))
    .unwrap()
}

/// [NodeJS only]
/// Blends multiple images read from local file system into one using `algorithm` or `algorithms` and the extra
/// `options` given. Algorithm defaults to `BlendAlgorithm::Multiplicative`.
#[wasm_bindgen(js_name = blendMultipleFs)]
pub fn blend_multiple_fs(
    image_paths: Box<[JsValue]>,
    out_path: String,
    algorithm: Option<String>,
    algorithms: Option<Box<[JsValue]>>,
    is_inline: Option<bool>,
    options: JsValue,
) -> Result<(), JsValue> {
    let num_images = image_paths.len();

    if num_images < 1 {
        return Err(PConvertError::ArgumentError(
            "ArgumentError: 'img_paths' must contain at least one path".to_string(),
        )
        .into());
    }

    let options = match options.is_object() {
        true => options.into_serde::<HashMap<String, JSONValue>>().ok(),
        false => None,
    };

    let algorithms_to_apply: Vec<(BlendAlgorithm, Option<BlendAlgorithmParams>)> =
        if let Some(algorithms) = algorithms {
            build_params(algorithms)?
        } else if let Some(algorithm) = algorithm {
            let algorithm = build_algorithm(&algorithm)?;
            vec![(algorithm, None); num_images - 1]
        } else {
            vec![(BlendAlgorithm::Multiplicative, None); num_images - 1]
        };

    if algorithms_to_apply.len() != num_images - 1 {
        return Err(PConvertError::ArgumentError(format!(
            "ArgumentError: 'algorithms' must be of size {} (one per blending operation)",
            num_images - 1
        ))
        .into());
    };

    let _is_inline = is_inline.unwrap_or(false);

    // loops through the algorithms to apply and blends the
    // current composition with the next layer
    let mut img_paths_iter = image_paths.iter();
    let first_path = img_paths_iter
        .next()
        .unwrap()
        .as_string()
        .expect("path must be a string");

    let node_fs = node_require("fs");

    let first_demultiply = is_algorithm_multiplied(&algorithms_to_apply[0].0);
    let composition = node_read_file_sync(&node_fs, &first_path);
    let mut composition = decode_png(&composition[..], first_demultiply)?;

    let mut zip_iter = img_paths_iter.zip(algorithms_to_apply.iter());
    while let Some(pair) = zip_iter.next() {
        let path = pair.0.as_string().expect("path must be a string");
        let (algorithm, algorithm_params) = pair.1;
        let demultiply = is_algorithm_multiplied(&algorithm);
        let algorithm_fn = get_blending_algorithm(&algorithm);
        let current_layer = node_read_file_sync(&node_fs, &path);
        let current_layer = decode_png(&current_layer[..], demultiply)?;
        blend_images(
            &mut composition,
            &current_layer,
            &algorithm_fn,
            algorithm_params,
        );
    }

    let compression_type = get_compression_type(&options);
    let filter_type = get_filter_type(&options);

    let mut encoded_data = Vec::<u8>::with_capacity(composition.to_vec().capacity());
    encode_png(
        &mut encoded_data,
        &composition,
        compression_type,
        filter_type,
    )?;

    node_write_file_sync(&node_fs, &out_path, &encoded_data);

    Ok(())
}

/// [NodeJS only]
/// Asynchronously blends multiple images read from local file system into one using `algorithm` or `algorithms` and the extra
/// `options` given. Algorithm defaults to `BlendAlgorithm::Multiplicative`.
#[wasm_bindgen(js_name = blendMultipleFsAsync)]
pub async fn blend_multiple_fs_async(
    image_paths: Box<[JsValue]>,
    out_path: String,
    algorithm: Option<String>,
    algorithms: Option<Box<[JsValue]>>,
    is_inline: Option<bool>,
    options: JsValue,
) -> Result<(), JsValue> {
    let num_images = image_paths.len();

    if num_images < 1 {
        return Err(PConvertError::ArgumentError(
            "ArgumentError: 'img_paths' must contain at least one path".to_string(),
        )
        .into());
    }

    let options = match options.is_object() {
        true => options.into_serde::<HashMap<String, JSONValue>>().ok(),
        false => None,
    };

    let algorithms_to_apply: Vec<(BlendAlgorithm, Option<BlendAlgorithmParams>)> =
        if let Some(algorithms) = algorithms {
            build_params(algorithms)?
        } else if let Some(algorithm) = algorithm {
            let algorithm = build_algorithm(&algorithm)?;
            vec![(algorithm, None); num_images - 1]
        } else {
            vec![(BlendAlgorithm::Multiplicative, None); num_images - 1]
        };

    if algorithms_to_apply.len() != num_images - 1 {
        return Err(PConvertError::ArgumentError(format!(
            "ArgumentError: 'algorithms' must be of size {} (one per blending operation)",
            num_images - 1
        ))
        .into());
    };

    let _is_inline = is_inline.unwrap_or(false);

    let node_fs = node_require("fs");

    let mut png_futures: Vec<Option<wasm_bindgen_futures::JsFuture>> =
        Vec::with_capacity(num_images);
    for path in image_paths.iter() {
        let path = path.as_string().expect("path must be a string");
        let png_future = node_read_file_async(&node_fs, &path);
        png_futures.push(Some(png_future));
    }

    let first_demultiply = is_algorithm_multiplied(&algorithms_to_apply[0].0);
    let composition = png_futures[0].take().unwrap().await?;
    let composition = js_sys::Uint8Array::from(composition).to_vec();
    let mut composition = decode_png(&composition[..], first_demultiply)?;

    // loops through the algorithms to apply and blends the
    // current composition with the next layer
    // retrieves the images from the result channels
    for i in 1..png_futures.len() {
        let (algorithm, algorithm_params) = &algorithms_to_apply[i - 1];
        let demultiply = is_algorithm_multiplied(&algorithm);
        let algorithm_fn = get_blending_algorithm(&algorithm);
        let current_layer = png_futures[i].take().unwrap().await?;
        let current_layer = js_sys::Uint8Array::from(current_layer).to_vec();
        let current_layer = decode_png(&current_layer[..], demultiply)?;

        blend_images(
            &mut composition,
            &current_layer,
            &algorithm_fn,
            algorithm_params,
        );
    }

    let compression_type = get_compression_type(&options);
    let filter_type = get_filter_type(&options);

    let mut encoded_data = Vec::<u8>::with_capacity(composition.to_vec().capacity());
    encode_png(
        &mut encoded_data,
        &composition,
        compression_type,
        filter_type,
    )?;

    node_write_file_sync(&node_fs, &out_path, &encoded_data);

    Ok(())
}

fn blend_multiple_buffers(
    image_buffers: Vec<ImageBuffer<Rgba<u8>, Vec<u8>>>,
    algorithm: Option<String>,
    algorithms: Option<Box<[JsValue]>>,
    is_inline: Option<bool>,
) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>, PConvertError> {
    let num_images = image_buffers.len();
    if num_images < 1 {
        return Err(PConvertError::ArgumentError(
            "'images' must contain at least one path".to_string(),
        ));
    }

    if algorithms.is_some() && algorithms.as_ref().unwrap().len() != num_images - 1 {
        return Err(PConvertError::ArgumentError(format!(
            "'algorithms' must be of size {} (one per blending operation)",
            num_images - 1
        )));
    };

    let _is_inline = is_inline.unwrap_or(false);

    let algorithms_to_apply: Vec<(BlendAlgorithm, Option<BlendAlgorithmParams>)> =
        if let Some(algorithms) = algorithms {
            build_params(algorithms)?
        } else if let Some(algorithm) = algorithm {
            let algorithm = build_algorithm(&algorithm)?;
            vec![(algorithm, None); num_images - 1]
        } else {
            vec![(BlendAlgorithm::Multiplicative, None); num_images - 1]
        };

    let mut image_buffers_iter = image_buffers.iter();
    let first_demultiply = is_algorithm_multiplied(&algorithms_to_apply[0].0);
    let mut composition = image_buffers_iter.next().unwrap().to_owned();
    if first_demultiply {
        demultiply_image(&mut composition);
    }
    let mut zip_iter = image_buffers_iter.zip(algorithms_to_apply.iter());
    while let Some(pair) = zip_iter.next() {
        let mut current_layer = pair.0.to_owned();
        let (algorithm, algorithm_params) = pair.1;
        let demultiply = is_algorithm_multiplied(&algorithm);
        let algorithm_fn = get_blending_algorithm(&algorithm);

        if demultiply {
            demultiply_image(&mut current_layer);
        }

        blend_images(
            &mut composition,
            &current_layer,
            &algorithm_fn,
            algorithm_params,
        );
    }

    Ok(composition)
}