Home | History | Annotate | Download | only in android
      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 
      8 #ifndef SkBitmapRegionDecoderPriv_DEFINED
      9 #define SkBitmapRegionDecoderPriv_DEFINED
     10 
     11 enum SubsetType {
     12     kFullyInside_SubsetType,
     13     kPartiallyInside_SubsetType,
     14     kOutside_SubsetType,
     15 };
     16 
     17 /*
     18  * Corrects image subset offsets and dimensions in order to perform a valid decode.
     19  * Also indicates if the image subset should be placed at an offset within the
     20  * output bitmap.
     21  *
     22  * Values of output variables are undefined if the SubsetType is kInvalid.
     23  *
     24  * @param imageDims Original image dimensions.
     25  * @param subset    As input, the subset that the client requested.
     26  *                  As output, the image subset that we will decode.
     27  * @param outX      The left offset of the image subset within the output bitmap.
     28  * @param outY      The top offset of the image subset within the output bitmap.
     29  *
     30  * @return An indication of how the subset is contained in the image.
     31  *         If the return value is kInvalid, values of output variables are undefined.
     32  */
     33 inline SubsetType adjust_subset_rect(const SkISize& imageDims, SkIRect* subset, int* outX,
     34         int* outY) {
     35     // These must be at least zero, we can't start decoding the image at a negative coordinate.
     36     int left = SkTMax(0, subset->fLeft);
     37     int top = SkTMax(0, subset->fTop);
     38 
     39     // If input offsets are less than zero, we decode to an offset location in the output bitmap.
     40     *outX = left - subset->fLeft;
     41     *outY = top - subset->fTop;
     42 
     43     // Make sure we don't decode pixels past the edge of the image or past the edge of the subset.
     44     int width = SkTMin(imageDims.width() - left, subset->width() - *outX);
     45     int height = SkTMin(imageDims.height() - top, subset->height() - *outY);
     46     if (width <= 0 || height <= 0) {
     47         return SubsetType::kOutside_SubsetType;
     48     }
     49 
     50     subset->setXYWH(left, top, width, height);
     51     if ((*outX != 0) || (*outY != 0) || (width != subset->width()) ||
     52             (height != subset->height())) {
     53         return SubsetType::kPartiallyInside_SubsetType;
     54     }
     55 
     56     return SubsetType::kFullyInside_SubsetType;
     57 }
     58 
     59 #endif // SkBitmapRegionDecoderPriv_DEFINED
     60