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
//! Create image collages.

extern crate image;
use image::{GenericImageView, DynamicImage, Rgba};
extern crate imageproc;
extern crate rusttype;
use wasm_bindgen::prelude::*;
use crate::{Rgb};
use crate::text::*;
use crate::elements::*;
use imageproc::drawing::draw_filled_rect_mut;
use imageproc::rect::Rect;

/// Two grid collage.
/// # Arguments
/// * `img` - A mutable ref to a DynamicImage.
/// * `img2` - A mutable ref to a DynamicImage.
/// * `width` - u32 - Desired width of final graphic 
/// * `height` - u32 - Desired height of final graphic
#[wasm_bindgen]
pub fn two_grid(photon_img: &DynamicImage, photon_img2: &DynamicImage, width: u32, height: u32) -> DynamicImage {
    // Convert all photon images to DynamicImages, for interop with the image crate.
    let imgs = vec![photon_img, photon_img2];

    // distribute the width evenly by allocating the same space to both images
    let img_width = width / 2;
    let imgs = resize_imgs(imgs, img_width, height);

    let mut container_img : DynamicImage = DynamicImage::new_rgba8(width, height);

    image::imageops::overlay(&mut container_img, &imgs[0], 0, 0);
    image::imageops::overlay(&mut container_img, &imgs[1], img_width, 0);

    // return the collage
    return container_img;
}

/// Four grid collage.
/// # Arguments
/// * `img` - A mutable ref to a DynamicImage.
/// * `img2` - A mutable ref to a DynamicImage.
/// * `main_text` - Main heading for the graphic.
/// * `width` - u32 - Desired width of final graphic 
/// * `height` - u32 - Desired height of final graphic
pub fn two_grid_text(image: DynamicImage, image2: DynamicImage, width: u32, height: u32) -> DynamicImage {
    // distribute the width evenly by allocating the same space to both images
    let img_width = width / 2;
    let img_height = height / 2;

    let sampling_filter = image::FilterType::Nearest;
    let image = image::ImageRgba8(image::imageops::resize(&image, img_width, img_height, sampling_filter));
    let image2 = image::ImageRgba8(image::imageops::resize(&image2, img_width, img_height, sampling_filter));

    let mut container_img : DynamicImage = DynamicImage::new_rgba8(width, height);

    image::imageops::overlay(&mut container_img, &image, 0, 0);
    image::imageops::overlay(&mut container_img, &image2, image.width(), image.height());

    let lilac = Rgb{r: 204, g: 195, b: 240};
    let yellow = Rgb{ r: 255, g: 226, b: 98};
    draw_solid_rect(&mut container_img, &yellow, img_height, img_width, image.width() as i32, 0);
    draw_solid_rect(&mut container_img, &lilac, img_height, img_width, 0, image.height() as i32);
    let rgb_white = Rgb { r: 255, g: 255, b: 255};

    draw_text(&mut container_img, "Daisies In the Underground", image.width() + 30, img_height / 2, "Roboto-Bold", 30.0, &rgb_white);
    
    return container_img;

}


/// Split-pane collage, with text on LHS and collage on RHS.
/// 
/// # Arguments
/// * `img` - A mutable ref to a DynamicImage.
/// * `img2` - A mutable ref to a DynamicImage.
/// * `main_text` - Main heading for the graphic.
/// * `width` - u32 - Desired width of final graphic 
/// * `height` - u32 - Desired height of final graphic
pub fn split_imgs_text(image: DynamicImage, image2: DynamicImage, width: u32, height: u32) -> DynamicImage {

    // distribute the width evenly by allocating the same space to both images
    let img_width = width / 2;
    let img_height = height / 2;

    let sampling_filter = image::FilterType::Nearest;
    let image = image::ImageRgba8(image::imageops::resize(&image, img_width, img_height, sampling_filter));
    let image2 = image::ImageRgba8(image::imageops::resize(&image2, img_width, img_height, sampling_filter));

    let mut container_img : DynamicImage = DynamicImage::new_rgba8(width, height);

    image::imageops::overlay(&mut container_img, &image, img_width, 0);
    image::imageops::overlay(&mut container_img, &image2, image.width(), image.height());

    let white = Rgb{r: 255, g: 255, b: 255};

    draw_solid_rect(&mut container_img, &white, img_height * 2, img_width, 0, 0);

    draw_text(&mut container_img, "Life Is An Adventure", 45, img_height / 2, "BebasKai", 80.0, &white);
    
    return container_img;

}

/// Four grid collage.
/// 
/// # Arguments
/// * `img` - A mutable ref to a DynamicImage.
/// * `img2` - A mutable ref to a DynamicImage.
/// * `img3` - A mutable ref to a DynamicImage.
/// * `img4` - A mutable ref to a DynamicImage.
/// * `width` - u32 - Desired width of final graphic 
/// * `height` - u32 - Desired height of final graphic
pub fn four_grid(photon_img: &DynamicImage, photon_img2: &DynamicImage, photon_img3: &DynamicImage, 
                photon_img4: &DynamicImage, width: u32, height: u32) -> DynamicImage {
    let imgs = vec![photon_img, photon_img2, photon_img3, photon_img4];

    // distribute the width evenly by allocating the same space to all images
    let img_width = width / 2;
    let img_height = height / 2;

    let imgs = resize_imgs(imgs, img_width, img_height);

    let mut container_img : DynamicImage = DynamicImage::new_rgba8(width, height);

    image::imageops::overlay(&mut container_img, &imgs[0], 0, 0);
    image::imageops::overlay(&mut container_img, &imgs[1], img_width, 0);
    image::imageops::overlay(&mut container_img, &imgs[2], 0, img_height);
    image::imageops::overlay(&mut container_img, &imgs[3], img_width, img_height);

    // return the collage
    return container_img;
}


/// Create a triple grid collage graphic.
/// 
/// # Arguments
/// * `img` - A mutable ref to a DynamicImage.
/// * `img2` - A mutable ref to a DynamicImage.
/// * `img3` - A mutable ref to a DynamicImage.
/// * `width` - u32 - Desired width of final graphic 
/// * `height` - u32 - Desired height of final graphic
pub fn triple_grid(photon_img: &DynamicImage, photon_img2: &DynamicImage, photon_img3: &DynamicImage, width: u32, height: u32) -> DynamicImage {
    let imgs = vec![photon_img, photon_img2, photon_img3];

    // distribute the width evenly by allocating the same space to both images
    let img_width = width / 3;
    let imgs = resize_imgs(imgs, img_width, height);

    let mut container_img : DynamicImage = DynamicImage::new_rgba8(width, height);

    image::imageops::overlay(&mut container_img, &imgs[0], 0, 0);
    image::imageops::overlay(&mut container_img, &imgs[1], img_width, 0);
    image::imageops::overlay(&mut container_img, &imgs[2], img_width * 2, 0);

    // return the collage
    return container_img;
}

/// Four-image collage with a centre square containing text.
/// 
/// # Arguments
/// * `img` - A mutable ref to a DynamicImage.
/// * `img2` - A mutable ref to a DynamicImage.
/// * `img3` - A mutable ref to a DynamicImage.
/// * `img4` - A mutable ref to a DynamicImage.
/// * `text` - Main heading for the graphic.
/// * `width` - u32 - Desired width of final graphic 
/// * `height` - u32 - Desired height of final graphic
pub fn four_grid_center_square(photon_img: &DynamicImage, photon_img2: &DynamicImage, photon_img3: &DynamicImage, 
                                photon_img4: &DynamicImage, text: &str, width: u32, height: u32) -> DynamicImage {
    let imgs = vec![photon_img, photon_img2, photon_img3, photon_img4];

    // distribute the width evenly by allocating the same space to both images
    let img_width = width / 2;
    let img_height = height / 2;

    let imgs = resize_imgs(imgs, img_width, img_height);

    let mut container_img : DynamicImage = DynamicImage::new_rgba8(width, height);

    image::imageops::overlay(&mut container_img, &imgs[0], 0, 0);
    image::imageops::overlay(&mut container_img, &imgs[1], img_width, 0);
    image::imageops::overlay(&mut container_img, &imgs[2], 0, img_height);
    image::imageops::overlay(&mut container_img, &imgs[3], img_width, img_height);

    let white_rgb = Rgb { r: 255, g: 255, b: 255};
    let black_rgb = Rgb { r: 0, g: 0, b: 0};
    // Draw a square in the center
    
    let mut height_mul: f32 = 0.2;
    let mut word_vec = vec![];
    // Split words
    for word in text.split_whitespace() {
        println!("> {}", word);
        if word.len() > 7 {
            word_vec.push(word);
            continue;
        }
        else {
            word_vec.push(word);
        }
    }
    draw_solid_rect(&mut container_img, &white_rgb, (width as f32 * 0.3) as u32, (height as f32 * 0.8) as u32, (width as f32 * 0.3) as i32, (height as f32 * 0.15) as i32);  
    
     for word in word_vec {
        draw_text(&mut container_img, word, (width as f32 * 0.32) as u32, (height as f32 * height_mul) as u32, "BebasKai", 100.0, &black_rgb );  
        height_mul += 0.15;
    }
    
    return container_img;
}


/// Create a moodboard style graphic with 4 images within a collage.
/// 
/// # Arguments
/// * `img` - A mutable ref to a DynamicImage.
/// * `img2` - A mutable ref to a DynamicImage.
/// * `img3` - A mutable ref to a DynamicImage.
/// * `img4` - A mutable ref to a DynamicImage.
/// * `text` - Main heading for the graphic.
/// * `width` - u32 - Desired width of final graphic 
/// * `height` - u32 - Desired height of final graphic
pub fn moodboard(photon_img: &DynamicImage, photon_img2: &DynamicImage, photon_img3: &DynamicImage, 
photon_img4: &DynamicImage, text: &str, width: u32, height: u32) -> DynamicImage {
    
    // Exclude the first image, since it will have different dimensions when resized.
    let imgs = vec![photon_img2, photon_img3, photon_img4];

    // distribute the width evenly by allocating the same space to both images
    let img_width = width / 4;
    let img_height = height / 4;

    let first_img_width: u32 = width - img_width;
    let sampling_filter = image::FilterType::Nearest;

    let image = image::ImageRgba8(image::imageops::resize(photon_img, first_img_width, (height as f32 * 0.8) as u32, sampling_filter));

    let imgs = resize_imgs(imgs, img_width, img_height);

    let mut container_img : DynamicImage = DynamicImage::new_rgba8(width, height);

    image::imageops::overlay(&mut container_img, &image, 0, 0);
    image::imageops::overlay(&mut container_img, &imgs[0], first_img_width, 0);
    image::imageops::overlay(&mut container_img, &imgs[1], first_img_width, img_height);
    image::imageops::overlay(&mut container_img, &imgs[2], first_img_width, img_height * 2);

    let white_rgb = Rgb { r: 255, g: 255, b: 255};
    let black_rgb = Rgb { r: 0, g: 0, b: 0};
    // Draw a square in the center
    
    let height_mul: f32 = 0.75;
    
    draw_solid_rect(&mut container_img, &white_rgb, width as u32, (height as f32 * 0.3) as u32, 0 as i32, (height as f32 * 0.75) as i32);  

    draw_text(&mut container_img, text, (width as f32 * 0.10) as u32, (height as f32 * height_mul) as u32, "Oswald-Regular", 100.0, &black_rgb );  
    
    return container_img;
}

/// Three-image collage containing main text, and a feature-style grid.
/// 
/// # Arguments
/// * `img` - A mutable ref to a DynamicImage.
/// * `img2` - A mutable ref to a DynamicImage.
/// * `img3` - A mutable ref to a DynamicImage.
/// * `text` - Main heading for the graphic.
/// * `width` - u32 - Desired width of final graphic 
/// * `height` - u32 - Desired height of final graphic
pub fn feature_grid(photon_img: &DynamicImage, photon_img2: &DynamicImage, photon_img3: &DynamicImage, 
                    main_text: &str, width: u32, height: u32) -> DynamicImage {
    let imgs = vec![photon_img, photon_img2, photon_img3];

    // distribute the width evenly by allocating the same space to all images
    let img_width = width / 2;
    let img_height = height / 2;

    let imgs = resize_imgs(imgs, img_width, img_height);

    let mut container_img : DynamicImage = DynamicImage::new_rgba8(width, height);
    let white = Rgb{r: 255, g: 255, b: 255};

    draw_filled_rect_mut(&mut container_img, 
                        Rect::at(0, 0).of_size( (width / 2) as u32, (height / 2) as u32), 
                        Rgba([white.r, white.g, 
                        white.b, 255u8]));

    image::imageops::overlay(&mut container_img, &imgs[0], img_width, 0);
    image::imageops::overlay(&mut container_img, &imgs[1], 0, img_height);
    image::imageops::overlay(&mut container_img, &imgs[2], img_width, img_height);

    let black_rgb = Rgb { r: 0, g: 0, b: 0};
    draw_text(&mut container_img, main_text, (width as f32 * 0.1) as u32, (height as f32 * 0.3) as u32, "BebasKai", 100.0, &black_rgb );  
    return container_img;
}

/// Triple-image collage with a centre square containing text.
/// 
/// # Arguments
/// * `img` - A mutable ref to a DynamicImage.
/// * `img2` - A mutable ref to a DynamicImage.
/// * `img3` - A mutable ref to a DynamicImage.
/// * `img4` - A mutable ref to a DynamicImage.
/// * `text` - Main heading for the graphic.
/// * `width` - u32 - Desired width of final graphic 
/// * `height` - u32 - Desired height of final graphic
pub fn triple_grid_text(photon_img: &DynamicImage, photon_img2: &DynamicImage, photon_img3: &DynamicImage, 
                        text: &str, width: u32, height: u32) -> DynamicImage {
    let imgs = vec![photon_img, photon_img2, photon_img3];

    // distribute the width evenly by allocating the same space to both images
    let img_width = width / 3;
    let img_height = (height as f32 * 0.8) as u32;
    let imgs = resize_imgs(imgs, img_width, img_height);

    let mut container_img : DynamicImage = DynamicImage::new_rgba8(width, height);
    let white = Rgb{r: 255, g: 255, b: 255};

    image::imageops::overlay(&mut container_img, &imgs[0], 0, 0);
    image::imageops::overlay(&mut container_img, &imgs[1], img_width, 0);
    image::imageops::overlay(&mut container_img, &imgs[2], img_width * 2, 0);

    let black_rgb = Rgb{ r: 0, g: 0, b:0 };

    draw_solid_rect(&mut container_img, &white, width as u32, (height as f32 * 0.3) as u32, 0 as i32, (height as f32 * 0.75) as i32);  
    draw_text(&mut container_img, text, (width as f32 * 0.05) as u32, (height as f32 * 0.8) as u32, "Montserrat-Regular", 90.0, &black_rgb );  

    return container_img;
}

pub fn six_grid_text(photon_img: &DynamicImage, photon_img2: &DynamicImage, photon_img3: &DynamicImage,
                    photon_img4: &DynamicImage, photon_img5: &DynamicImage, photon_img6: &DynamicImage, 
                    text: &str, width: u32, height: u32) -> DynamicImage {
    
    let imgs = vec![photon_img, photon_img2, photon_img3, photon_img4, photon_img5, photon_img6];

    // distribute the width evenly by allocating the same space to both images
    let img_width = width / 3;
    let img_height = height / 3;
    let imgs = resize_imgs(imgs, img_width, img_height);

    let mut container_img : DynamicImage = DynamicImage::new_rgba8(width, height);
    let white = Rgb{r: 255, g: 255, b: 255};

    image::imageops::overlay(&mut container_img, &imgs[0], 0, 0);
    image::imageops::overlay(&mut container_img, &imgs[1], img_width, 0);
    image::imageops::overlay(&mut container_img, &imgs[2], img_width * 2, 0);
    image::imageops::overlay(&mut container_img, &imgs[3], 0, img_height * 2);
    image::imageops::overlay(&mut container_img, &imgs[4], img_width, img_height * 2);
    image::imageops::overlay(&mut container_img, &imgs[5], img_width * 2, img_height * 2);


    let black_rgb = Rgb{ r: 0, g: 0, b:0 };

    draw_solid_rect(&mut container_img, &white, width as u32, img_height, 0 as i32, img_height as i32);  
    draw_text(&mut container_img, text, (width as f32 * 0.05) as u32, (img_height + (img_height as f32 * 0.3) as u32) as u32, "Montserrat-Regular", 90.0, &black_rgb );  

    return container_img;
}



// Resize images in a vec, returns a new vec with resized images.
fn resize_imgs(imgs: Vec<&DynamicImage>, img_width: u32, img_height: u32) -> Vec<DynamicImage> {
    let sampling_filter = image::FilterType::Nearest;
    let mut resized_imgs = vec![];

    for i in 0..imgs.len() {
        let item = imgs[i];
        let image = image::ImageRgba8(image::imageops::resize(item, img_width, img_height, sampling_filter));

        resized_imgs.push(image);
    }

    return resized_imgs;
}