Skip to content

Commit fa02783

Browse files
shinmili38
authored andcommitted
Make internal rect filler not draw bottom/right edges
1 parent 1387a5b commit fa02783

File tree

3 files changed

+52
-36
lines changed

3 files changed

+52
-36
lines changed

plotters-bitmap/src/bitmap/test.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn test_bitmap_backend_fill_half() {
4646
for y in 0..10 {
4747
assert_eq!(
4848
buffer[(y * 10 + x) as usize * 3 + 0],
49-
if x <= 5 { 255 } else { 0 }
49+
if x < 5 { 255 } else { 0 }
5050
);
5151
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 1], 0);
5252
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 2], 0);
@@ -67,7 +67,7 @@ fn test_bitmap_backend_fill_half() {
6767
for y in 0..10 {
6868
assert_eq!(
6969
buffer[(y * 10 + x) as usize * 3 + 0],
70-
if y <= 5 { 255 } else { 0 }
70+
if y < 5 { 255 } else { 0 }
7171
);
7272
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 1], 0);
7373
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 2], 0);
@@ -95,7 +95,7 @@ fn test_bitmap_backend_blend() {
9595

9696
for x in 0..10 {
9797
for y in 0..10 {
98-
let (r, g, b) = if x <= 5 {
98+
let (r, g, b) = if x < 5 {
9999
(205, 225, 245)
100100
} else {
101101
(255, 255, 255)
@@ -157,6 +157,22 @@ fn test_draw_rect_out_of_range() {
157157
}
158158
}
159159

160+
#[cfg(test)]
161+
#[test]
162+
fn test_draw_rect_exclude_bottom_right() {
163+
use plotters::prelude::*;
164+
let mut buffer = vec![0; 100 * 100 * 3];
165+
166+
{
167+
let mut back = BitMapBackend::with_buffer(&mut buffer, (100, 100));
168+
169+
back.draw_rect((0, 0), (0, 0), &RED.to_rgba(), true)
170+
.unwrap();
171+
}
172+
173+
assert_eq!(&buffer[0..3], &[0, 0, 0]);
174+
}
175+
160176
#[cfg(test)]
161177
#[test]
162178
fn test_draw_line_out_of_range() {
@@ -203,7 +219,7 @@ fn test_bitmap_blend_large() {
203219

204220
for x in 0..1000 {
205221
for y in 0..1000 {
206-
let expected_value = if x <= 100 && y <= 100 {
222+
let expected_value = if x < 100 && y < 100 {
207223
let (r, g, b) = fill_color.to_rgba().rgb();
208224
(
209225
if r > 0 { 139 } else { 12 },

plotters-bitmap/src/bitmap_pixel/bgrx.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ impl PixelFormat for BGRXPixel {
4646
upper_left.1.min(bottom_right.1).max(0),
4747
);
4848
let (x1, y1) = (
49-
upper_left.0.max(bottom_right.0).min(w as i32 - 1),
50-
upper_left.1.max(bottom_right.1).min(h as i32 - 1),
49+
upper_left.0.max(bottom_right.0).min(w as i32),
50+
upper_left.1.max(bottom_right.1).min(h as i32),
5151
);
5252

5353
// This may happen when the minimal value is larger than the limit.
5454
// Thus we just have something that is completely out-of-range
55-
if x0 > x1 || y0 > y1 {
55+
if x0 >= x1 || y0 >= y1 {
5656
return;
5757
}
5858

@@ -79,9 +79,9 @@ impl PixelFormat for BGRXPixel {
7979
const N: u64 = 0xff00_ff00_ff00_ff00;
8080
const M: u64 = 0x00ff_00ff_00ff_00ff;
8181

82-
for y in y0..=y1 {
82+
for y in y0..y1 {
8383
let start = (y * w as i32 + x0) as usize;
84-
let count = (x1 - x0 + 1) as usize;
84+
let count = (x1 - x0) as usize;
8585

8686
let start_ptr = &mut dst[start * Self::PIXEL_SIZE] as *mut u8 as *mut [u8; 8];
8787
let slice = unsafe { std::slice::from_raw_parts_mut(start_ptr, (count - 1) / 2) };
@@ -135,54 +135,54 @@ impl PixelFormat for BGRXPixel {
135135
upper_left.1.min(bottom_right.1).max(0),
136136
);
137137
let (x1, y1) = (
138-
upper_left.0.max(bottom_right.0).min(w as i32 - 1),
139-
upper_left.1.max(bottom_right.1).min(h as i32 - 1),
138+
upper_left.0.max(bottom_right.0).min(w as i32),
139+
upper_left.1.max(bottom_right.1).min(h as i32),
140140
);
141141

142142
// This may happen when the minimal value is larger than the limit.
143143
// Thus we just have something that is completely out-of-range
144-
if x0 > x1 || y0 > y1 {
144+
if x0 >= x1 || y0 >= y1 {
145145
return;
146146
}
147147

148148
let dst = target.get_raw_pixel_buffer();
149149

150150
if r == g && g == b {
151151
// If r == g == b, then we can use memset
152-
if x0 != 0 || x1 != w as i32 - 1 {
152+
if x0 != 0 || x1 != w as i32 {
153153
// If it's not the entire row is filled, we can only do
154154
// memset per row
155-
for y in y0..=y1 {
155+
for y in y0..y1 {
156156
let start = (y * w as i32 + x0) as usize;
157-
let count = (x1 - x0 + 1) as usize;
157+
let count = (x1 - x0) as usize;
158158
dst[(start * Self::PIXEL_SIZE)..((start + count) * Self::PIXEL_SIZE)]
159159
.iter_mut()
160160
.for_each(|e| *e = r);
161161
}
162162
} else {
163163
// If the entire memory block is going to be filled, just use single memset
164164
dst[Self::PIXEL_SIZE * (y0 * w as i32) as usize
165-
..((y1 + 1) * w as i32) as usize * Self::PIXEL_SIZE]
165+
..(y1 * w as i32) as usize * Self::PIXEL_SIZE]
166166
.iter_mut()
167167
.for_each(|e| *e = r);
168168
}
169169
} else {
170-
let count = (x1 - x0 + 1) as usize;
170+
let count = (x1 - x0) as usize;
171171
if count < 8 {
172-
for y in y0..=y1 {
172+
for y in y0..y1 {
173173
let start = (y * w as i32 + x0) as usize;
174174
let mut iter = dst
175175
[(start * Self::PIXEL_SIZE)..((start + count) * Self::PIXEL_SIZE)]
176176
.iter_mut();
177-
for _ in 0..=(x1 - x0) {
177+
for _ in 0..count {
178178
*iter.next().unwrap() = b;
179179
*iter.next().unwrap() = g;
180180
*iter.next().unwrap() = r;
181181
iter.next();
182182
}
183183
}
184184
} else {
185-
for y in y0..=y1 {
185+
for y in y0..y1 {
186186
let start = (y * w as i32 + x0) as usize;
187187
let start_ptr = &mut dst[start * Self::PIXEL_SIZE] as *mut u8 as *mut [u8; 8];
188188
let slice =

plotters-bitmap/src/bitmap_pixel/rgb.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ impl PixelFormat for RGBPixel {
5050
upper_left.1.min(bottom_right.1).max(0),
5151
);
5252
let (x1, y1) = (
53-
upper_left.0.max(bottom_right.0).min(w as i32 - 1),
54-
upper_left.1.max(bottom_right.1).min(h as i32 - 1),
53+
upper_left.0.max(bottom_right.0).min(w as i32),
54+
upper_left.1.max(bottom_right.1).min(h as i32),
5555
);
5656

5757
// This may happen when the minimal value is larger than the limit.
5858
// Thus we just have something that is completely out-of-range
59-
if x0 > x1 || y0 > y1 {
59+
if x0 >= x1 || y0 >= y1 {
6060
return;
6161
}
6262

@@ -87,9 +87,9 @@ impl PixelFormat for RGBPixel {
8787
const N: u64 = 0xff00_ff00_ff00_ff00;
8888
const M: u64 = 0x00ff_00ff_00ff_00ff;
8989

90-
for y in y0..=y1 {
90+
for y in y0..y1 {
9191
let start = (y * w as i32 + x0) as usize;
92-
let count = (x1 - x0 + 1) as usize;
92+
let count = (x1 - x0) as usize;
9393

9494
let start_ptr = &mut dst[start * Self::PIXEL_SIZE] as *mut u8 as *mut [u8; 24];
9595
let slice = unsafe { std::slice::from_raw_parts_mut(start_ptr, (count - 1) / 8) };
@@ -150,53 +150,53 @@ impl PixelFormat for RGBPixel {
150150
upper_left.1.min(bottom_right.1).max(0),
151151
);
152152
let (x1, y1) = (
153-
upper_left.0.max(bottom_right.0).min(w as i32 - 1),
154-
upper_left.1.max(bottom_right.1).min(h as i32 - 1),
153+
upper_left.0.max(bottom_right.0).min(w as i32),
154+
upper_left.1.max(bottom_right.1).min(h as i32),
155155
);
156156

157157
// This may happen when the minimal value is larger than the limit.
158158
// Thus we just have something that is completely out-of-range
159-
if x0 > x1 || y0 > y1 {
159+
if x0 >= x1 || y0 >= y1 {
160160
return;
161161
}
162162

163163
let dst = target.get_raw_pixel_buffer();
164164

165165
if r == g && g == b {
166166
// If r == g == b, then we can use memset
167-
if x0 != 0 || x1 != w as i32 - 1 {
167+
if x0 != 0 || x1 != w as i32 {
168168
// If it's not the entire row is filled, we can only do
169169
// memset per row
170-
for y in y0..=y1 {
170+
for y in y0..y1 {
171171
let start = (y * w as i32 + x0) as usize;
172-
let count = (x1 - x0 + 1) as usize;
172+
let count = (x1 - x0) as usize;
173173
dst[(start * Self::PIXEL_SIZE)..((start + count) * Self::PIXEL_SIZE)]
174174
.iter_mut()
175175
.for_each(|e| *e = r);
176176
}
177177
} else {
178178
// If the entire memory block is going to be filled, just use single memset
179179
dst[Self::PIXEL_SIZE * (y0 * w as i32) as usize
180-
..((y1 + 1) * w as i32) as usize * Self::PIXEL_SIZE]
180+
..(y1 * w as i32) as usize * Self::PIXEL_SIZE]
181181
.iter_mut()
182182
.for_each(|e| *e = r);
183183
}
184184
} else {
185-
let count = (x1 - x0 + 1) as usize;
185+
let count = (x1 - x0) as usize;
186186
if count < 8 {
187-
for y in y0..=y1 {
187+
for y in y0..y1 {
188188
let start = (y * w as i32 + x0) as usize;
189189
let mut iter = dst
190190
[(start * Self::PIXEL_SIZE)..((start + count) * Self::PIXEL_SIZE)]
191191
.iter_mut();
192-
for _ in 0..=(x1 - x0) {
192+
for _ in 0..count {
193193
*iter.next().unwrap() = r;
194194
*iter.next().unwrap() = g;
195195
*iter.next().unwrap() = b;
196196
}
197197
}
198198
} else {
199-
for y in y0..=y1 {
199+
for y in y0..y1 {
200200
let start = (y * w as i32 + x0) as usize;
201201
let start_ptr = &mut dst[start * Self::PIXEL_SIZE] as *mut u8 as *mut [u8; 24];
202202
let slice =

0 commit comments

Comments
 (0)