Skip to content

Commit afedbf9

Browse files
committed
Move methods to apply and remove filter to Png
1 parent 721aa1b commit afedbf9

File tree

2 files changed

+81
-25
lines changed

2 files changed

+81
-25
lines changed

crates/png-glitch/src/lib.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl PngGlitch {
237237
/// png_glitch.save("./etc/removed-all.png").expect("The PNG file should be successfully saved")
238238
/// ```
239239
pub fn remove_filter(&mut self) {
240-
self.remove_filter_from(0, self.height());
240+
self.png.remove_filter();
241241
}
242242

243243
/// The method removes filter from the scan lines in specified region
@@ -251,20 +251,7 @@ impl PngGlitch {
251251
/// png_glitch.save("./etc/removed-partial.png").expect("The PNG file should be successfully saved")
252252
/// ```
253253
pub fn remove_filter_from(&mut self, from: u32, lines: u32) {
254-
let index = if from > 0 { from - 1 } else { 0 };
255-
let mut lines = self.scan_lines_from(index, lines);
256-
lines.reverse();
257-
258-
let mut previous = if from > 0 {
259-
lines.pop()
260-
} else {
261-
None
262-
};
263-
while !lines.is_empty() {
264-
let last_index = lines.len() - 1;
265-
lines[last_index].remove_filter(previous.as_ref());
266-
previous = lines.pop()
267-
}
254+
self.png.remove_filter_from(from, lines);
268255
}
269256

270257
/// The method removes filter from all scan lines.
@@ -278,7 +265,7 @@ impl PngGlitch {
278265
/// png_glitch.save("./etc/filter-all.png").expect("The PNG file should be successfully saved")
279266
/// ```
280267
pub fn apply_filter(&mut self, filter: FilterType) {
281-
self.apply_filter_from(filter, 0, self.height());
268+
self.png.apply_filter(filter);
282269
}
283270

284271
/// The method removes filter from scan lines in specified region
@@ -292,15 +279,7 @@ impl PngGlitch {
292279
/// png_glitch.save("./etc/filter-partial.png").expect("The PNG file should be successfully saved")
293280
/// ```
294281
pub fn apply_filter_from(&mut self, filter_type: FilterType, from: u32, lines: u32) {
295-
let mut lines = self.scan_lines_from(from, lines);
296-
let mut previous = lines.pop();
297-
298-
while !lines.is_empty() {
299-
if let Some(mut line) = previous {
300-
previous = lines.pop();
301-
line.apply_filter(filter_type, previous.as_ref());
302-
}
303-
}
282+
self.png.apply_filter_from(filter_type, from, lines);
304283
}
305284

306285
}

crates/png-glitch/src/png.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,83 @@ impl Png {
8484
let end = start + self.scan_line_width() * lines as usize;
8585
start..end
8686
}
87+
88+
/// The method removes filter from all scan lines.
89+
///
90+
/// # Example
91+
///
92+
/// ```
93+
/// use png_glitch::PngGlitch;
94+
/// let mut png_glitch = PngGlitch::open("./etc/sample00.png").expect("The PNG file should be successfully parsed");
95+
/// png_glitch.remove_filter();
96+
/// png_glitch.save("./etc/removed-all.png").expect("The PNG file should be successfully saved")
97+
/// ```
98+
pub fn remove_filter(&mut self) {
99+
self.remove_filter_from(0, self.height());
100+
}
101+
102+
/// The method removes filter from the scan lines in specified region
103+
///
104+
/// # Example
105+
///
106+
/// ```
107+
/// use png_glitch::PngGlitch;
108+
/// let mut png_glitch = PngGlitch::open("./etc/sample00.png").expect("The PNG file should be successfully parsed");
109+
/// png_glitch.remove_filter_from(5, 10); // Remove filter from the scan line #5 - # 14
110+
/// png_glitch.save("./etc/removed-partial.png").expect("The PNG file should be successfully saved")
111+
/// ```
112+
pub fn remove_filter_from(&mut self, from: u32, lines: u32) {
113+
let index = if from > 0 { from - 1 } else { 0 };
114+
let mut lines = self.scan_lines_from(index as usize, lines as usize);
115+
lines.reverse();
116+
117+
let mut previous = if from > 0 {
118+
lines.pop()
119+
} else {
120+
None
121+
};
122+
while !lines.is_empty() {
123+
let last_index = lines.len() - 1;
124+
lines[last_index].remove_filter(previous.as_ref());
125+
previous = lines.pop()
126+
}
127+
}
128+
129+
/// The method removes filter from all scan lines.
130+
///
131+
/// # Example
132+
///
133+
/// ```
134+
/// use png_glitch::{FilterType, PngGlitch};
135+
/// let mut png_glitch = PngGlitch::open("./etc/none.png").expect("The PNG file should be successfully parsed");
136+
/// png_glitch.apply_filter(FilterType::Sub);
137+
/// png_glitch.save("./etc/filter-all.png").expect("The PNG file should be successfully saved")
138+
/// ```
139+
pub fn apply_filter(&mut self, filter: FilterType) {
140+
self.apply_filter_from(filter, 0, self.height());
141+
}
142+
143+
/// The method removes filter from scan lines in specified region
144+
///
145+
/// # Example
146+
///
147+
/// ```
148+
/// use png_glitch::{FilterType, PngGlitch};
149+
/// let mut png_glitch = PngGlitch::open("./etc/none.png").expect("The PNG file should be successfully parsed");
150+
/// png_glitch.apply_filter_from(FilterType::Sub, 5, 3); // Apply sub filter to the scan line #5, #6, and #7.
151+
/// png_glitch.save("./etc/filter-partial.png").expect("The PNG file should be successfully saved")
152+
/// ```
153+
pub fn apply_filter_from(&mut self, filter_type: FilterType, from: u32, lines: u32) {
154+
let mut lines = self.scan_lines_from(from as usize, lines as usize);
155+
let mut previous = lines.pop();
156+
157+
while !lines.is_empty() {
158+
if let Some(mut line) = previous {
159+
previous = lines.pop();
160+
line.apply_filter(filter_type, previous.as_ref());
161+
}
162+
}
163+
}
87164
}
88165

89166
impl TryFrom<&[u8]> for Png {

0 commit comments

Comments
 (0)