msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 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 | |
Leon Scroggins III | 87caae6 | 2020-05-04 10:02:45 -0400 | [diff] [blame] | 8 | #ifndef BitmapRegionDecoderPriv_DEFINED |
| 9 | #define BitmapRegionDecoderPriv_DEFINED |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkRect.h" |
Hal Canary | 6b20a55 | 2017-02-07 14:09:38 -0500 | [diff] [blame] | 12 | |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 13 | enum SubsetType { |
| 14 | kFullyInside_SubsetType, |
| 15 | kPartiallyInside_SubsetType, |
| 16 | kOutside_SubsetType, |
| 17 | }; |
| 18 | |
| 19 | /* |
| 20 | * Corrects image subset offsets and dimensions in order to perform a valid decode. |
| 21 | * Also indicates if the image subset should be placed at an offset within the |
| 22 | * output bitmap. |
| 23 | * |
| 24 | * Values of output variables are undefined if the SubsetType is kInvalid. |
| 25 | * |
| 26 | * @param imageDims Original image dimensions. |
| 27 | * @param subset As input, the subset that the client requested. |
| 28 | * As output, the image subset that we will decode. |
| 29 | * @param outX The left offset of the image subset within the output bitmap. |
| 30 | * @param outY The top offset of the image subset within the output bitmap. |
| 31 | * |
| 32 | * @return An indication of how the subset is contained in the image. |
| 33 | * If the return value is kInvalid, values of output variables are undefined. |
| 34 | */ |
| 35 | inline SubsetType adjust_subset_rect(const SkISize& imageDims, SkIRect* subset, int* outX, |
| 36 | int* outY) { |
| 37 | // These must be at least zero, we can't start decoding the image at a negative coordinate. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 38 | int left = std::max(0, subset->fLeft); |
| 39 | int top = std::max(0, subset->fTop); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 40 | |
| 41 | // If input offsets are less than zero, we decode to an offset location in the output bitmap. |
| 42 | *outX = left - subset->fLeft; |
| 43 | *outY = top - subset->fTop; |
| 44 | |
| 45 | // Make sure we don't decode pixels past the edge of the image or past the edge of the subset. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 46 | int width = std::min(imageDims.width() - left, subset->width() - *outX); |
| 47 | int height = std::min(imageDims.height() - top, subset->height() - *outY); |
msarett | 26ad17b | 2015-10-22 07:29:19 -0700 | [diff] [blame] | 48 | if (width <= 0 || height <= 0) { |
| 49 | return SubsetType::kOutside_SubsetType; |
| 50 | } |
| 51 | |
| 52 | subset->setXYWH(left, top, width, height); |
| 53 | if ((*outX != 0) || (*outY != 0) || (width != subset->width()) || |
| 54 | (height != subset->height())) { |
| 55 | return SubsetType::kPartiallyInside_SubsetType; |
| 56 | } |
| 57 | |
| 58 | return SubsetType::kFullyInside_SubsetType; |
| 59 | } |
| 60 | |
Leon Scroggins III | 87caae6 | 2020-05-04 10:02:45 -0400 | [diff] [blame] | 61 | #endif // BitmapRegionDecoderPriv_DEFINED |