1 /*
2 * Copyright 2023 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 #include "src/core/SkReadPixelsRec.h"
8
9 #include "include/core/SkRect.h"
10
trim(int srcWidth,int srcHeight)11 bool SkReadPixelsRec::trim(int srcWidth, int srcHeight) {
12 if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) {
13 return false;
14 }
15 if (0 >= fInfo.width() || 0 >= fInfo.height()) {
16 return false;
17 }
18
19 int x = fX;
20 int y = fY;
21 SkIRect srcR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height());
22 if (!srcR.intersect({0, 0, srcWidth, srcHeight})) {
23 return false;
24 }
25
26 // if x or y are negative, then we have to adjust pixels
27 if (x > 0) {
28 x = 0;
29 }
30 if (y > 0) {
31 y = 0;
32 }
33 // here x,y are either 0 or negative
34 // we negate and add them so UBSAN (pointer-overflow) doesn't get confused.
35 fPixels = ((char*)fPixels + -y*fRowBytes + -x*fInfo.bytesPerPixel());
36 // the intersect may have shrunk info's logical size
37 fInfo = fInfo.makeDimensions(srcR.size());
38 fX = srcR.x();
39 fY = srcR.y();
40
41 return true;
42 }
43