SkBitmapRegionDecoderPriv.h 2.2 KB

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