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
8 #include "src/core/SkWritePixelsRec.h"
9
10 #include "include/core/SkRect.h"
11
trim(int dstWidth,int dstHeight)12 bool SkWritePixelsRec::trim(int dstWidth, int dstHeight) {
13 if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) {
14 return false;
15 }
16 if (0 >= fInfo.width() || 0 >= fInfo.height()) {
17 return false;
18 }
19
20 int x = fX;
21 int y = fY;
22 SkIRect dstR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height());
23 if (!dstR.intersect({0, 0, dstWidth, dstHeight})) {
24 return false;
25 }
26
27 // if x or y are negative, then we have to adjust pixels
28 if (x > 0) {
29 x = 0;
30 }
31 if (y > 0) {
32 y = 0;
33 }
34 // here x,y are either 0 or negative
35 // we negate and add them so UBSAN (pointer-overflow) doesn't get confused.
36 fPixels = ((const char*)fPixels + -y*fRowBytes + -x*fInfo.bytesPerPixel());
37 // the intersect may have shrunk info's logical size
38 fInfo = fInfo.makeDimensions(dstR.size());
39 fX = dstR.x();
40 fY = dstR.y();
41
42 return true;
43 }
44