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 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
//! Special effects. extern crate image; use image::{GenericImage, GenericImageView}; use std::f64; extern crate imageproc; use imageproc::drawing::draw_filled_rect_mut; use imageproc::rect::Rect; extern crate rusttype; use crate::{PhotonImage, Rgb}; use crate::helpers; use wasm_bindgen::prelude::*; use image::{Rgba}; /// Adds an offset to the image by a certain number of pixels. /// /// # Arguments /// * `img` - A PhotonImage that contains a view into the image. /// * `offset` - The offset is added to the pixels in the image. /// # Example /// /// ``` /// // For example, to offset pixels by 30 pixels on the red channel: /// use photon::effects; /// photon::effects::offset(img, 0, 30); /// ``` #[wasm_bindgen] pub fn offset(photon_image: &mut PhotonImage, channel_index: usize, offset: u32) { if channel_index > 2 { panic!("Invalid channel index passed. Channel1 must be equal to 0, 1, or 2."); } let mut img = helpers::dyn_image_from_raw(&photon_image); let (width, height) = img.dimensions(); for x in 0..width - 10 { for y in 0..height - 10 { let mut px = img.get_pixel(x, y); if x + offset < width - 1 && y + offset < height - 1 { let offset_px = img.get_pixel(x + offset, y + offset); px.data[channel_index] = offset_px.data[channel_index]; } img.put_pixel(x, y, px); } } let raw_pixels = img.raw_pixels(); photon_image.raw_pixels = raw_pixels; } /// Adds an offset to the red channel by a certain number of pixels. /// /// # Arguments /// * `img` - A PhotonImage that contains a view into the image. /// * `offset` - The offset you want to move the red channel by. /// # Example /// /// ``` /// // For example, to add an offset to the red channel by 30 pixels. /// use photon::effects; /// photon::effects::offset_red(img, 30); /// ``` #[wasm_bindgen] pub fn offset_red(img: &mut PhotonImage, offset_amt: u32) { offset(img, 0, offset_amt) } /// Adds an offset to the green channel by a certain number of pixels. /// /// # Arguments /// * `img` - A PhotonImage that contains a view into the image. /// * `offset` - The offset you want to move the green channel by. /// # Example /// /// ``` /// // For example, to add an offset to the green channel by 30 pixels. /// use photon::effects; /// photon::effects::offset_green(img, 40); /// ``` #[wasm_bindgen] pub fn offset_green(img: &mut PhotonImage, offset_amt: u32) { offset(img, 1, offset_amt) } /// Adds an offset to the blue channel by a certain number of pixels. /// /// # Arguments /// * `img` - A PhotonImage that contains a view into the image. /// * `offset_amt` - The offset you want to move the blue channel by. /// # Example /// // For example, to add an offset to the green channel by 40 pixels. /// use photon::effects; /// photon::effects::offset_blue(img, 40); /// ``` #[wasm_bindgen] pub fn offset_blue(img: &mut PhotonImage, offset_amt: u32) { offset(img, 2, offset_amt) } /// Adds multiple offsets to the image by a certain number of pixels (on two channels). /// /// # Arguments /// * `img` - A PhotonImage that contains a view into the image. /// * `offset` - The offset is added to the pixels in the image. /// # Example /// /// ``` /// // For example, to add a 30-pixel offset to both the red and blue channels: /// use photon::effects; /// photon::effects::multiple_offsets(img, 30, 0, 2); /// ``` #[wasm_bindgen] pub fn multiple_offsets(mut photon_image: &mut PhotonImage, offset: u32, channel_index: usize, channel_index2: usize) { if channel_index > 2 { panic!("Invalid channel index passed. Channel1 must be equal to 0, 1, or 2."); } if channel_index2 > 2 { panic!("Invalid channel index passed. Channel2 must be equal to 0, 1, or 2."); } let mut img = helpers::dyn_image_from_raw(&photon_image); let (width, height) = img.dimensions(); for x in 0..width { for y in 0..height { let mut px = img.get_pixel(x, y); if x + offset < width - 1 && y + offset < height - 1 { let offset_px = img.get_pixel(x + offset, y); px.data[channel_index] = offset_px.data[channel_index]; } if x as i32 - offset as i32 > 0 && y as i32 - offset as i32 > 0 { let offset_px2 = img.get_pixel(x - offset, y ); px.data[channel_index2] = offset_px2.data[channel_index2]; } img.put_pixel(x, y, px); } } let raw_pixels = img.raw_pixels(); photon_image.raw_pixels = raw_pixels; } /// Halftoning effect. pub fn halftone(mut photon_image: PhotonImage) { let mut img = helpers::dyn_image_from_raw(&photon_image); let (width, height) = img.dimensions(); for x in (0..width).step_by(2 as usize) { for y in (0..height).step_by(2 as usize) { let mut px1 = img.get_pixel(x, y); let mut px2 = img.get_pixel(x, y + 1); let mut px3 = img.get_pixel(x + 1, y); let mut px4 = img.get_pixel(x + 1, y + 1); let gray1 = (px1[0] as f64 * 0.299) + (px1[1] as f64 * 0.587) + (px1[2] as f64 * 0.114); let gray2 = (px2[0] as f64 * 0.299) + (px2[1] as f64 * 0.587) + (px2[2] as f64 * 0.114); let gray3 = (px3[0] as f64 * 0.299) + (px3[1] as f64 * 0.587) + (px3[2] as f64 * 0.114); let gray4 = (px4[0] as f64 * 0.299) + (px4[1] as f64 * 0.587) + (px4[2] as f64 * 0.114); let sat = (gray1 + gray2 + gray3 + gray4) / 4.0; if sat > 200.0 { px1.data[0] = 255; px1.data[1] = 255; px1.data[2] = 255; px2.data[0] = 255; px2.data[1] = 255; px2.data[2] = 255; px3.data[0] = 255; px3.data[1] = 255; px3.data[2] = 255; px4.data[0] = 255; px4.data[1] = 255; px4.data[2] = 255; } else if sat > 159.0 { px1.data[0] = 255; px1.data[1] = 255; px1.data[2] = 255; px2.data[0] = 0; px2.data[1] = 0; px2.data[2] = 0; px3.data[0] = 255; px3.data[1] = 255; px3.data[2] = 255; px4.data[0] = 255; px4.data[1] = 255; px4.data[2] = 255; } else if sat > 95.0 { px1.data[0] = 255; px1.data[1] = 255; px1.data[2] = 255; px2.data[0] = 0; px2.data[1] = 0; px2.data[2] = 0; px3.data[0] = 0; px3.data[1] = 0; px3.data[2] = 0; px4.data[0] = 255; px4.data[1] = 255; px4.data[2] = 255; } else if sat > 32.0 { px1.data[0] = 0; px1.data[1] = 0; px1.data[2] = 0; px2.data[0] = 255; px2.data[0] = 255; px2.data[0] = 255; px3.data[0] = 0; px3.data[1] = 0; px3.data[2] = 0; px4.data[0] = 0; px4.data[1] = 0; px4.data[2] = 0; } else { px1.data[0] = 0; px1.data[1] = 0; px1.data[2] = 0; px2.data[0] = 0; px2.data[1] = 0; px2.data[2] = 0; px3.data[0] = 0; px3.data[1] = 0; px3.data[2] = 0; px4.data[0] = 0; px4.data[1] = 0; px4.data[2] = 0; } img.put_pixel(x, y, px1); // img.put_pixel(x, y + 1, px2); } } let raw_pixels = img.raw_pixels(); photon_image.raw_pixels = raw_pixels; } /// Reduces an image to the primary colours. /// /// # Arguments /// * `img` - A PhotonImage that contains a view into the image. /// # Example /// /// ``` /// // For example, to add a primary colour effect to an image of type `DynamicImage`: /// use photon::effects; /// photon::effects::primary(img); /// ``` #[wasm_bindgen] pub fn primary(mut photon_image: &mut PhotonImage) { let mut img = helpers::dyn_image_from_raw(&photon_image); let (width, height) = img.dimensions(); for x in 0..width{ for y in 0..height { let mut px = img.get_pixel(x, y); let mut r_val = px.data[0]; let mut g_val = px.data[1]; let mut b_val = px.data[2]; if r_val > 128 { r_val = 255; } else { r_val = 0; } if g_val > 128 { g_val = 255; } else { g_val = 0; } if b_val > 128 { g_val = 255; } else { b_val = 0; } px.data[0] = r_val; px.data[1] = g_val; px.data[2] = b_val; img.put_pixel(x, y, px); } } let raw_pixels = img.raw_pixels(); photon_image.raw_pixels = raw_pixels; } /// Colorizes the green channels of the image. /// /// # Arguments /// * `img` - A PhotonImage that contains a view into the image. /// # Example /// /// ``` /// // For example, to colorize an image of type `PhotonImage`: /// use photon::effects; /// photon::effects::colorize(img); /// ``` #[wasm_bindgen] pub fn colorize(mut photon_image: &mut PhotonImage) { let mut img = helpers::dyn_image_from_raw(&photon_image); let threshold = 220; let (width, height) = img.dimensions(); for x in 0..width { for y in 0..height { let mut px = img.get_pixel(x, y); let px_as_rgb = Rgb{r: px.data[0], g: px.data[1], b: px.data[2]}; let baseline_color = Rgb{r: 0, g: 255, b: 255}; let square_distance = crate::helpers::square_distance(baseline_color, px_as_rgb); let mut r = px.data[0] as f32; let mut g = px.data[1] as f32; let mut b = px.data[2] as f32; if square_distance < i32::pow(threshold, 2) { r = r * 0.5; g = g * 1.25; b = b * 0.5; } px.data[0] = r as u8; px.data[1] = g as u8; px.data[2] = b as u8; img.put_pixel(x, y, px); } } let raw_pixels = img.raw_pixels(); photon_image.raw_pixels = raw_pixels; } // #[wasm_bindgen] // pub fn inc_luminosity(mut photon_image: PhotonImage) -> PhotonImage { // let mut img = helpers::dyn_image_from_raw(&photon_image); // let (width, height) = img.dimensions(); // let mut min_intensity = 255; // let mut max_intensity = 0; // // find the max and min intensities in the image // for x in 0..width { // for y in 0..height { // let px = img.get_pixel(x, y); // let intensity = (px.data[0] as u32 + px.data[1] as u32 + px.data[2] as u32) / 3; // if intensity > 0{ // min_intensity = cmp::min(min_intensity, intensity); // max_intensity = cmp::max(max_intensity, intensity); // } // } // } // for x in 0..width { // for y in 0..height { // let mut px = img.get_pixel(x, y); // // let px_as_rgb = Rgb{r: px.data[0], g: px.data[1], b: px.data[2]}; // let mut r = px.data[0] as f32; // let mut g = px.data[1] as f32; // let mut b = px.data[2] as f32; // let lum = (r + g + b) / 3.0; // let new_lum = 255.0 * (lum - min_intensity as f32) / (max_intensity / min_intensity) as f32; // r = r * new_lum / lum; // g = g * new_lum / lum; // b = b * new_lum / lum; // px.data[0] = r as u8; // px.data[1] = g as u8; // px.data[2] = b as u8; // img.put_pixel(x, y, px); // } // } // let mut raw_pixels = img.raw_pixels(); // photon_image.raw_pixels = raw_pixels; // return photon_image; // } /// Applies a solarizing effect to an image. /// /// # Arguments /// * `img` - A PhotonImage that contains a view into the image. /// # Example /// /// ``` /// // For example, to colorize an image of type `PhotonImage`: /// use photon::effects; /// photon::effects::solarize(img); /// ``` #[wasm_bindgen] pub fn solarize(mut photon_image: &mut PhotonImage) { let mut img = helpers::dyn_image_from_raw(&photon_image); let (width, height) = img.dimensions(); for x in 0..width { for y in 0..height { let mut px = img.get_pixel(x, y); if 200 as i32 - px.data[0] as i32 > 0 { px.data[0] = 200 - px.data[0]; } img.put_pixel(x, y, px); } } let raw_pixels = img.raw_pixels(); photon_image.raw_pixels = raw_pixels; } /// Applies a solarizing effect to an image and returns the resulting PhotonImage. /// /// # Arguments /// * `img` - A PhotonImage that contains a view into the image. /// # Example /// /// ``` /// // For example, to solarize an image of type `PhotonImage`: /// use photon::effects; /// let img = photon::effects::solarize(img); /// ``` #[wasm_bindgen] pub fn solarize_retimg(photon_image: &PhotonImage) -> PhotonImage { let mut img = helpers::dyn_image_from_raw(&photon_image); let (width, height) = img.dimensions(); for x in 0..width { for y in 0..height { let mut px = img.get_pixel(x, y); if 200 as i32 - px.data[0] as i32 > 0 { px.data[0] = 200 - px.data[0]; } img.put_pixel(x, y, px); } } let new_photon_image = PhotonImage { raw_pixels: img.raw_pixels(), width: img.width(), height: img.height()}; return new_photon_image } /// Increase the brightness of an image by a factor. /// /// # Arguments /// * `img` - A PhotonImage that contains a view into the image. /// * `brightness` - A u8 to add to the brightness. /// # Example /// /// ``` /// photon::effects::inc_brightness(img, 10); /// ``` #[wasm_bindgen] pub fn inc_brightness(mut photon_image: &mut PhotonImage, brightness: u8) { let mut img = helpers::dyn_image_from_raw(&photon_image); let (width, height) = img.dimensions(); for x in 0..width { for y in 0..height { let mut px = img.get_pixel(x, y); if px.data[0] <= 255 - brightness { px.data[0] += brightness; } else { px.data[0] = 255; } if px.data[1] <= 255 - brightness { px.data[1] += brightness; } else { px.data[1] = 255 } if px.data[2] <= 255 - brightness { px.data[2] += brightness; } else { px.data[2] = 255 } img.put_pixel(x, y, px); } } photon_image.raw_pixels = img.raw_pixels(); } /// Adjust the contrast of an image by a factor. /// /// # Arguments /// * `photon_image` - A PhotonImage that contains a view into the image. /// * `contrast` - An f32 factor used to adjust contrast. Between [-255.0, 255.0]. The algorithm will /// clamp results if passed factor is out of range. /// # Example /// /// ``` /// photon::effects::adjust_contrast(photon_image, 30.0); /// ``` #[wasm_bindgen] pub fn adjust_contrast(mut photon_image: &mut PhotonImage, contrast: f32) { let mut img = helpers::dyn_image_from_raw(&photon_image); let (width, height) = img.dimensions(); let clamped_contrast = num::clamp(contrast, -255.0, 255.0); // Some references: // https://math.stackexchange.com/questions/906240/algorithms-to-increase-or-decrease-the-contrast-of-an-image // https://www.dfstudios.co.uk/articles/programming/image-programming-algorithms/image-processing-algorithms-part-5-contrast-adjustment/ let factor = (259.0 * (clamped_contrast + 255.0)) / (255.0 * (259.0 - clamped_contrast)); let mut lookup_table: Vec<u8> = vec![0; 256]; let offset = -128.0 * factor + 128.0; for i in 0..256 { let new_val = i as f32 * factor + offset; lookup_table[i] = num::clamp(new_val, 0.0, 255.0) as u8; } for x in 0..width { for y in 0..height { let mut px = img.get_pixel(x, y); px.data[0] = lookup_table[px.data[0] as usize]; px.data[1] = lookup_table[px.data[1] as usize]; px.data[2] = lookup_table[px.data[2] as usize]; img.put_pixel(x, y, px); } } photon_image.raw_pixels = img.raw_pixels(); } /// Tint an image by adding an offset to averaged RGB channel values. /// /// # Arguments /// * `img` - A PhotonImage that contains a view into the image. /// * `r_offset` - The amount the R channel should be incremented by. /// * `g_offset` - The amount the G channel should be incremented by. /// * `b_offset` - The amount the B channel should be incremented by. /// # Example /// /// ``` /// // For example, to tint an image of type `PhotonImage`: /// photon::tint(img, 10, 20, 15); /// ``` /// #[wasm_bindgen] pub fn tint(mut photon_image: &mut PhotonImage, r_offset: u32, g_offset: u32, b_offset: u32) { let mut img = helpers::dyn_image_from_raw(&photon_image); let (width, height) = img.dimensions(); for x in 0..width { for y in 0..height { let mut px = img.get_pixel(x, y); let (r_val, g_val, b_val) = (px.data[0] as u32, px.data[1] as u32, px.data[2] as u32); px.data[0] = if r_val as u32 + r_offset < 255 { r_val as u8 + r_offset as u8} else { 255 }; px.data[1] = if g_val as u32 + g_offset < 255 { g_val as u8 + g_offset as u8} else { 255 }; px.data[2] = if b_val as u32 + b_offset < 255 { b_val as u8 + b_offset as u8} else { 255 }; img.put_pixel(x, y, px); } } let raw_pixels = img.raw_pixels(); photon_image.raw_pixels = raw_pixels; } /// Horizontal strips. Divide an image into a series of equal-height strips, for an artistic effect. #[wasm_bindgen] pub fn horizontal_strips(mut photon_image: &mut PhotonImage, num_strips: u8) { let mut img = helpers::dyn_image_from_raw(&photon_image); let (width, height) = img.dimensions(); let total_strips = (num_strips * 2) - 1; let height_strip = height / total_strips as u32; let background_color = Rgb { r: 255, g: 255, b: 255}; let mut y_pos: u32 = 0; for i in 1..num_strips { draw_filled_rect_mut(&mut img, Rect::at(0, (y_pos + height_strip) as i32).of_size(width, height_strip), Rgba([background_color.r, background_color.g, background_color.b, 255u8])); y_pos = i as u32 * (height_strip * 2); } let raw_pixels = img.raw_pixels(); photon_image.raw_pixels = raw_pixels; } /// Vertical strips. Divide an image into a series of equal-width strips, for an artistic effect. #[wasm_bindgen] pub fn vertical_strips(mut photon_image: &mut PhotonImage, num_strips: u8) { let mut img = helpers::dyn_image_from_raw(&photon_image); let (width, height) = img.dimensions(); let total_strips = (num_strips * 2) - 1; let width_strip = width / total_strips as u32; let background_color = Rgb { r: 255, g: 255, b: 255}; let mut x_pos: u32 = 0; for i in 1..num_strips { draw_filled_rect_mut(&mut img, Rect::at((x_pos + width_strip) as i32, 0).of_size(width_strip, height), Rgba([background_color.r, background_color.g, background_color.b, 255u8])); x_pos = i as u32 * (width_strip * 2); } let raw_pixels = img.raw_pixels(); photon_image.raw_pixels = raw_pixels; } // pub fn create_gradient_map(color_a : Rgb, color_b: Rgb) -> Vec<Rgb> { // println!("hi"); // println!("{}", color_a.get_red()); // let mut gradient_map = vec![]; // let max_val = 255; // let mut r_val = 0; // let end: i32 = 256 * 4; // for i in (0..end).step_by(4){ // let i: u8 = i as u8; // let intensity_b = max_val - i; // let res1 = (i * color_a.get_red() + intensity_b * color_b.get_red()); // let res2 = res1 / max_val; // println!("res 1 {}", res1); // println!("res 2 {}", res2); // r_val = (i * color_a.get_red() + intensity_b * color_b.get_red()) / max_val; // println!("r_val {}", r_val); // gradient_map.push(Rgb { // r: (256 - (i / 4) * color_a.get_red() + (i / 4) * color_b.r) / 256 , // g: (i * color_a.get_green() + intensity_b * color_b.get_green()) / max_val , // b: (i * color_a.get_blue() + intensity_b * color_b.get_blue()) / max_val // }); // } // println!("{:?}", gradient_map); // return gradient_map; // } // pub fn duotone(mut img: DynamicImage, color_a : Rgb, color_b : Rgb) -> DynamicImage { // let (width, height) = img.dimensions(); // let gradient_map = create_gradient_map(color_a, color_b); // println!("entering for loop"); // for x in 0..width { // for y in 0..height { // let mut px = img.get_pixel(x, y); // let r = px.data[0]; // let g = px.data[1]; // let b = px.data[2]; // px.data[0] = gradient_map[r as usize].r as u8; // px.data[1] = gradient_map[g as usize].g as u8; // px.data[2] = gradient_map[b as usize].b as u8; // img.put_pixel(x, y, px); // } // } // return img; // }