aboutsummaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2018-11-01 03:29:06 -0400
committerGitHub <noreply@github.com>2018-11-01 03:29:06 -0400
commit8aded34bf6cff6cb657bf434afdd9dc6cd1c4020 (patch)
treeab27d5f6337a0a7d9d72f4869e09758f3a51e0b8 /components
parent126ed26171c2788f0012aafdc50042d96a9fc696 (diff)
parentc2be78b3feace2a93132a167d7791dce5289053b (diff)
downloadservo-8aded34bf6cff6cb657bf434afdd9dc6cd1c4020.tar.gz
servo-8aded34bf6cff6cb657bf434afdd9dc6cd1c4020.zip
Auto merge of #21914 - Eijebong:canvas-cleanup, r=jdm
Use pixels::get_rect in CanvasData::draw_image Those functions are the same except that get_rect isn't failible, so we have to check that the rect we want is actually in the image. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21914) <!-- Reviewable:end -->
Diffstat (limited to 'components')
-rw-r--r--components/canvas/canvas_data.rs42
1 files changed, 5 insertions, 37 deletions
diff --git a/components/canvas/canvas_data.rs b/components/canvas/canvas_data.rs
index 0ce0b352ccc..2b42aba9656 100644
--- a/components/canvas/canvas_data.rs
+++ b/components/canvas/canvas_data.rs
@@ -68,7 +68,11 @@ impl<'a> CanvasData<'a> {
// We round up the floating pixel values to draw the pixels
let source_rect = source_rect.ceil();
// It discards the extra pixels (if any) that won't be painted
- let image_data = crop_image(image_data, image_size, source_rect);
+ let image_data = if Rect::from_size(image_size).contains_rect(&source_rect) {
+ pixels::get_rect(&image_data, image_size.to_u32(), source_rect.to_u32()).into()
+ } else {
+ image_data.into()
+ };
let writer = |draw_target: &DrawTarget| {
write_image(&draw_target, image_data, source_rect.size, dest_rect,
@@ -577,42 +581,6 @@ fn is_zero_size_gradient(pattern: &Pattern) -> bool {
false
}
-/// Used by drawImage to get rid of the extra pixels of the image data that
-/// won't be copied to the canvas
-/// image_data: Color pixel data of the image
-/// image_size: Image dimensions
-/// crop_rect: It determines the area of the image we want to keep
-fn crop_image(
- image_data: Vec<u8>,
- image_size: Size2D<f64>,
- crop_rect: Rect<f64>
-) -> Vec<u8> {
- // We're going to iterate over a pixel values array so we need integers
- let crop_rect = crop_rect.to_i32();
- let image_size = image_size.to_i32();
- if crop_rect == Rect::from_size(image_size) {
- return image_data;
- }
- // Assuming 4 bytes per pixel and row-major order for storage
- // (consecutive elements in a pixel row of the image are contiguous in memory)
- let stride = image_size.width * 4;
- let image_bytes_length = image_size.height * image_size.width * 4;
- let crop_area_bytes_length = crop_rect.size.height * crop_rect.size.width * 4;
- // If the image size is less or equal than the crop area we do nothing
- if image_bytes_length <= crop_area_bytes_length {
- return image_data;
- }
-
- let mut new_image_data = Vec::new();
- let mut src = (crop_rect.origin.y * stride + crop_rect.origin.x * 4) as usize;
- for _ in 0..crop_rect.size.height {
- let row = &image_data[src .. src + (4 * crop_rect.size.width) as usize];
- new_image_data.extend_from_slice(row);
- src += stride as usize;
- }
- new_image_data
-}
-
/// It writes an image to the destination target
/// draw_target: the destination target where the image_data will be copied
/// image_data: Pixel information of the image to be written. It takes RGBA8