File size: 8,726 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use super::{ActionLabel, ActionLabelSet, ImageProperty, PropertyOutput};

pub struct ActionLabelUtil;

impl ActionLabelUtil {
    pub fn is_output_size_same_as_input_size(action_label_set: &ActionLabelSet) -> bool {
        let mut same_width = false;
        let mut same_height = false;
        for label in action_label_set {
            match label {
                ActionLabel::OutputPropertyIsEqualToInputProperty { output, input } => {
                    if *output == PropertyOutput::OutputWidth && *input == ImageProperty::Width {
                        same_width = true;
                    }
                    if *output == PropertyOutput::OutputHeight && *input == ImageProperty::Height {
                        same_height = true;
                    }
                },
                _ => {}
            }
        }
        same_width && same_height
    }

    pub fn is_output_size_same_as_removed_rectangle_after_single_color_removal(action_label_set: &ActionLabelSet) -> bool {
        let mut same_width = false;
        let mut same_height = false;
        for label in action_label_set {
            match label {
                ActionLabel::OutputPropertyIsEqualToInputProperty { output, input } => {
                    if *output == PropertyOutput::OutputWidth && *input == ImageProperty::WidthOfRemovedRectangleAfterSingleColorRemoval {
                        same_width = true;
                    }
                    if *output == PropertyOutput::OutputHeight && *input == ImageProperty::HeightOfRemovedRectangleAfterSingleColorRemoval {
                        same_height = true;
                    }
                },
                _ => {}
            }
        }
        same_width && same_height
    }

    pub fn is_output_size_same_as_input_splitview(action_label_set: &ActionLabelSet) -> bool {
        if Self::is_output_size_same_as_input_splitview_x(action_label_set) {
            return true;
        }
        if Self::is_output_size_same_as_input_splitview_y(action_label_set) {
            return true;
        }

        // Future experiments:
        // Detect if the output size is the same as the input's splitview part size, but rotated.
        // Detect if there is both splits in both directions x and y.

        // if Self::experimental_is_output_size_same_as_input_splitview_rotated(action_label_set) {
        //     return true;
        // }
        false
    }

    /// The input is multiple images layouted horizontally, and the output width equals `split part size x`.
    pub fn is_output_size_same_as_input_splitview_x(action_label_set: &ActionLabelSet) -> bool {
        let mut same_width = false;
        let mut same_height = false;
        for label in action_label_set {
            match label {
                ActionLabel::OutputPropertyIsEqualToInputProperty { output, input } => {
                    if *output == PropertyOutput::OutputWidth && *input == ImageProperty::SplitPartSizeX {
                        same_width = true;
                    }
                    if *output == PropertyOutput::OutputHeight && *input == ImageProperty::Height {
                        same_height = true;
                    }
                },
                _ => {}
            }
        }
        same_width && same_height
    }

    /// The input is multiple images layouted vertically, and the output height equals `split part size y`.
    pub fn is_output_size_same_as_input_splitview_y(action_label_set: &ActionLabelSet) -> bool {
        let mut same_width = false;
        let mut same_height = false;
        for label in action_label_set {
            match label {
                ActionLabel::OutputPropertyIsEqualToInputProperty { output, input } => {
                    if *output == PropertyOutput::OutputWidth && *input == ImageProperty::Width {
                        same_width = true;
                    }
                    if *output == PropertyOutput::OutputHeight && *input == ImageProperty::SplitPartSizeY {
                        same_height = true;
                    }
                },
                _ => {}
            }
        }
        same_width && same_height
    }

    #[allow(dead_code)]
    fn experimental_is_output_size_same_as_input_splitview_rotated(action_label_set: &ActionLabelSet) -> bool {
        let mut width_is_rotated = false;
        let mut height_is_rotated = false;
        for label in action_label_set {
            match label {
                ActionLabel::OutputPropertyIsEqualToInputProperty { output, input } => {
                    if *output == PropertyOutput::OutputWidth && *input == ImageProperty::SplitPartSizeY {
                        width_is_rotated = true;
                    }
                    if *output == PropertyOutput::OutputHeight && *input == ImageProperty::SplitPartSizeX {
                        height_is_rotated = true;
                    }
                },
                _ => {}
            }
        }
        width_is_rotated || height_is_rotated
    }

}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_10000_is_output_size_same_as_input_size_yes() {
        // Arrange
        let mut action_label_set = ActionLabelSet::new();
        {
            let label = ActionLabel::OutputPropertyIsEqualToInputProperty { 
                output: PropertyOutput::OutputWidth, 
                input: ImageProperty::Width 
            };
            action_label_set.insert(label);
        }
        {
            let label = ActionLabel::OutputPropertyIsEqualToInputProperty { 
                output: PropertyOutput::OutputHeight,
                input: ImageProperty::Height
            };
            action_label_set.insert(label);
        }

        // Act
        let actual: bool = ActionLabelUtil::is_output_size_same_as_input_size(&action_label_set);

        // Assert
        assert_eq!(actual, true);
    }

    #[test]
    fn test_10001_is_output_size_same_as_input_size_no() {
        // Arrange
        let mut action_label_set = ActionLabelSet::new();
        {
            let label = ActionLabel::OutputPropertyIsEqualToInputProperty { 
                output: PropertyOutput::OutputWidth, 
                input: ImageProperty::Height
            };
            action_label_set.insert(label);
        }
        {
            let label = ActionLabel::OutputPropertyIsEqualToInputProperty { 
                output: PropertyOutput::OutputHeight,
                input: ImageProperty::Width
            };
            action_label_set.insert(label);
        }

        // Act
        let actual: bool = ActionLabelUtil::is_output_size_same_as_input_size(&action_label_set);

        // Assert
        assert_eq!(actual, false);
    }

    #[test]
    fn test_20000_is_output_size_same_as_removed_rectangle_after_single_color_removal_yes() {
        // Arrange
        let mut action_label_set = ActionLabelSet::new();
        {
            let label = ActionLabel::OutputPropertyIsEqualToInputProperty { 
                output: PropertyOutput::OutputWidth, 
                input: ImageProperty::WidthOfRemovedRectangleAfterSingleColorRemoval 
            };
            action_label_set.insert(label);
        }
        {
            let label = ActionLabel::OutputPropertyIsEqualToInputProperty { 
                output: PropertyOutput::OutputHeight,
                input: ImageProperty::HeightOfRemovedRectangleAfterSingleColorRemoval
            };
            action_label_set.insert(label);
        }

        // Act
        let actual: bool = ActionLabelUtil::is_output_size_same_as_removed_rectangle_after_single_color_removal(&action_label_set);

        // Assert
        assert_eq!(actual, true);
    }

    #[test]
    fn test_20001_is_output_size_same_as_removed_rectangle_after_single_color_removal_no() {
        // Arrange
        let mut action_label_set = ActionLabelSet::new();
        {
            let label = ActionLabel::OutputPropertyIsEqualToInputProperty { 
                output: PropertyOutput::OutputWidth, 
                input: ImageProperty::HeightOfRemovedRectangleAfterSingleColorRemoval
            };
            action_label_set.insert(label);
        }
        {
            let label = ActionLabel::OutputPropertyIsEqualToInputProperty { 
                output: PropertyOutput::OutputHeight,
                input: ImageProperty::WidthOfRemovedRectangleAfterSingleColorRemoval
            };
            action_label_set.insert(label);
        }

        // Act
        let actual: bool = ActionLabelUtil::is_output_size_same_as_removed_rectangle_after_single_color_removal(&action_label_set);

        // Assert
        assert_eq!(actual, false);
    }
}