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
/*
 * Copyright (c) 2023.
 *
 * This software is free software;
 *
 * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
 */

//! Overloadable operators
//!
//! This module provides overloadable operators for the image struct
//!
//! The operations panic in case of the incompatibility between two images
//! so it is best you ensure the image struct is the same
//!
use core::ops::*;

use zune_core::bit_depth::BitType;

use crate::image::Image;

#[track_caller]
fn check_full_compatibility(img1: &Image, img2: &Image) {
    assert_eq!(
        img1.depth(),
        img2.depth(),
        "Image depth mismatch cannot continue {:?}!={:?}",
        img1.depth(),
        img2.depth()
    );

    assert_eq!(
        img1.dimensions(),
        img2.dimensions(),
        "Dimensions mismatch, {:?} != {:?}",
        img1.dimensions(),
        img2.dimensions()
    );

    assert_eq!(
        img1.colorspace(),
        img2.colorspace(),
        "Colorspace mismatch, {:?} != {:?}",
        img1.colorspace(),
        img2.colorspace()
    );
}

impl Add for Image {
    type Output = Image;

    fn add(self, rhs: Image) -> Self::Output {
        check_full_compatibility(&self, &rhs);

        let mut new_img = self;

        match new_img.depth().bit_type() {
            BitType::U8 => {
                for (prev, rhs_c) in new_img
                    .channels_mut(true)
                    .iter_mut()
                    .zip(rhs.channels_ref(true))
                {
                    let channel_px = prev.reinterpret_as_mut::<u8>().unwrap();
                    let channel_rhs = rhs_c.reinterpret_as::<u8>().unwrap();

                    channel_px
                        .iter_mut()
                        .zip(channel_rhs.iter())
                        .for_each(|(x, y)| *x = x.saturating_add(*y));
                }
            }
            BitType::U16 => {
                for (prev, rhs_c) in new_img
                    .channels_mut(true)
                    .iter_mut()
                    .zip(rhs.channels_ref(true))
                {
                    let channel_px = prev.reinterpret_as_mut::<u16>().unwrap();
                    let channel_rhs = rhs_c.reinterpret_as::<u16>().unwrap();

                    channel_px
                        .iter_mut()
                        .zip(channel_rhs.iter())
                        .for_each(|(x, y)| *x = x.saturating_add(*y));
                }
            }
            BitType::F32 => {
                for (prev, rhs_c) in new_img
                    .channels_mut(true)
                    .iter_mut()
                    .zip(rhs.channels_ref(true))
                {
                    let channel_px = prev.reinterpret_as_mut::<f32>().unwrap();
                    let channel_rhs = rhs_c.reinterpret_as::<f32>().unwrap();

                    channel_px
                        .iter_mut()
                        .zip(channel_rhs.iter())
                        .for_each(|(x, y)| *x += *y);
                }
            }
            d => unimplemented!("Unimplemented for {:?}", d)
        }
        new_img
    }
}

impl Sub for Image {
    type Output = Image;

    fn sub(self, rhs: Image) -> Self::Output {
        check_full_compatibility(&self, &rhs);

        let mut new_img = self;

        match new_img.depth().bit_type() {
            BitType::U8 => {
                for (prev, rhs_c) in new_img
                    .channels_mut(true)
                    .iter_mut()
                    .zip(rhs.channels_ref(true))
                {
                    let channel_px = prev.reinterpret_as_mut::<u8>().unwrap();
                    let channel_rhs = rhs_c.reinterpret_as::<u8>().unwrap();

                    channel_px
                        .iter_mut()
                        .zip(channel_rhs.iter())
                        .for_each(|(x, y)| *x = x.saturating_sub(*y));
                }
            }
            BitType::U16 => {
                for (prev, rhs_c) in new_img
                    .channels_mut(true)
                    .iter_mut()
                    .zip(rhs.channels_ref(true))
                {
                    let channel_px = prev.reinterpret_as_mut::<u16>().unwrap();
                    let channel_rhs = rhs_c.reinterpret_as::<u16>().unwrap();

                    channel_px
                        .iter_mut()
                        .zip(channel_rhs.iter())
                        .for_each(|(x, y)| *x = x.saturating_sub(*y));
                }
            }
            BitType::F32 => {
                for (prev, rhs_c) in new_img
                    .channels_mut(true)
                    .iter_mut()
                    .zip(rhs.channels_ref(true))
                {
                    let channel_px = prev.reinterpret_as_mut::<f32>().unwrap();
                    let channel_rhs = rhs_c.reinterpret_as::<f32>().unwrap();

                    channel_px
                        .iter_mut()
                        .zip(channel_rhs.iter())
                        .for_each(|(x, y)| *x -= *y);
                }
            }
            d => unimplemented!("Unimplemented for {:?}", d)
        }
        new_img
    }
}

#[test]
fn add() {
    let im = Image::fill(0_u8, zune_core::colorspace::ColorSpace::RGBA, 100, 100);
    let _ = im.clone() + im;
}