SkImageInfo.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 2010 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. #include "include/private/SkImageInfoPriv.h"
  8. #include "src/core/SkReadBuffer.h"
  9. #include "src/core/SkSafeMath.h"
  10. #include "src/core/SkWriteBuffer.h"
  11. int SkColorTypeBytesPerPixel(SkColorType ct) {
  12. switch (ct) {
  13. case kUnknown_SkColorType: return 0;
  14. case kAlpha_8_SkColorType: return 1;
  15. case kRGB_565_SkColorType: return 2;
  16. case kARGB_4444_SkColorType: return 2;
  17. case kRGBA_8888_SkColorType: return 4;
  18. case kBGRA_8888_SkColorType: return 4;
  19. case kRGB_888x_SkColorType: return 4;
  20. case kRGBA_1010102_SkColorType: return 4;
  21. case kRGB_101010x_SkColorType: return 4;
  22. case kGray_8_SkColorType: return 1;
  23. case kRGBA_F16Norm_SkColorType: return 8;
  24. case kRGBA_F16_SkColorType: return 8;
  25. case kRGBA_F32_SkColorType: return 16;
  26. }
  27. return 0;
  28. }
  29. bool SkColorTypeIsAlwaysOpaque(SkColorType ct) {
  30. return !(kAlpha_SkColorTypeComponentFlag & SkColorTypeComponentFlags(ct));
  31. }
  32. ///////////////////////////////////////////////////////////////////////////////////////////////////
  33. int SkImageInfo::bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
  34. int SkImageInfo::shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
  35. size_t SkImageInfo::computeOffset(int x, int y, size_t rowBytes) const {
  36. SkASSERT((unsigned)x < (unsigned)this->width());
  37. SkASSERT((unsigned)y < (unsigned)this->height());
  38. return SkColorTypeComputeOffset(this->colorType(), x, y, rowBytes);
  39. }
  40. size_t SkImageInfo::computeByteSize(size_t rowBytes) const {
  41. if (0 == this->height()) {
  42. return 0;
  43. }
  44. SkSafeMath safe;
  45. size_t bytes = safe.add(safe.mul(safe.addInt(this->height(), -1), rowBytes),
  46. safe.mul(this->width(), this->bytesPerPixel()));
  47. return safe ? bytes : SIZE_MAX;
  48. }
  49. SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) {
  50. return SkImageInfo(width, height, kN32_SkColorType, at,
  51. SkColorSpace::MakeSRGB());
  52. }
  53. bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
  54. SkAlphaType* canonical) {
  55. switch (colorType) {
  56. case kUnknown_SkColorType:
  57. alphaType = kUnknown_SkAlphaType;
  58. break;
  59. case kAlpha_8_SkColorType:
  60. if (kUnpremul_SkAlphaType == alphaType) {
  61. alphaType = kPremul_SkAlphaType;
  62. }
  63. // fall-through
  64. case kARGB_4444_SkColorType:
  65. case kRGBA_8888_SkColorType:
  66. case kBGRA_8888_SkColorType:
  67. case kRGBA_1010102_SkColorType:
  68. case kRGBA_F16Norm_SkColorType:
  69. case kRGBA_F16_SkColorType:
  70. case kRGBA_F32_SkColorType:
  71. if (kUnknown_SkAlphaType == alphaType) {
  72. return false;
  73. }
  74. break;
  75. case kGray_8_SkColorType:
  76. case kRGB_565_SkColorType:
  77. case kRGB_888x_SkColorType:
  78. case kRGB_101010x_SkColorType:
  79. alphaType = kOpaque_SkAlphaType;
  80. break;
  81. default:
  82. return false;
  83. }
  84. if (canonical) {
  85. *canonical = alphaType;
  86. }
  87. return true;
  88. }
  89. ///////////////////////////////////////////////////////////////////////////////////////////////////
  90. #include "src/image/SkReadPixelsRec.h"
  91. bool SkReadPixelsRec::trim(int srcWidth, int srcHeight) {
  92. if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) {
  93. return false;
  94. }
  95. if (0 >= fInfo.width() || 0 >= fInfo.height()) {
  96. return false;
  97. }
  98. int x = fX;
  99. int y = fY;
  100. SkIRect srcR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height());
  101. if (!srcR.intersect(0, 0, srcWidth, srcHeight)) {
  102. return false;
  103. }
  104. // if x or y are negative, then we have to adjust pixels
  105. if (x > 0) {
  106. x = 0;
  107. }
  108. if (y > 0) {
  109. y = 0;
  110. }
  111. // here x,y are either 0 or negative
  112. // we negate and add them so UBSAN (pointer-overflow) doesn't get confused.
  113. fPixels = ((char*)fPixels + -y*fRowBytes + -x*fInfo.bytesPerPixel());
  114. // the intersect may have shrunk info's logical size
  115. fInfo = fInfo.makeWH(srcR.width(), srcR.height());
  116. fX = srcR.x();
  117. fY = srcR.y();
  118. return true;
  119. }
  120. ///////////////////////////////////////////////////////////////////////////////////////////////////
  121. #include "src/core/SkWritePixelsRec.h"
  122. bool SkWritePixelsRec::trim(int dstWidth, int dstHeight) {
  123. if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) {
  124. return false;
  125. }
  126. if (0 >= fInfo.width() || 0 >= fInfo.height()) {
  127. return false;
  128. }
  129. int x = fX;
  130. int y = fY;
  131. SkIRect dstR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height());
  132. if (!dstR.intersect(0, 0, dstWidth, dstHeight)) {
  133. return false;
  134. }
  135. // if x or y are negative, then we have to adjust pixels
  136. if (x > 0) {
  137. x = 0;
  138. }
  139. if (y > 0) {
  140. y = 0;
  141. }
  142. // here x,y are either 0 or negative
  143. // we negate and add them so UBSAN (pointer-overflow) doesn't get confused.
  144. fPixels = ((const char*)fPixels + -y*fRowBytes + -x*fInfo.bytesPerPixel());
  145. // the intersect may have shrunk info's logical size
  146. fInfo = fInfo.makeWH(dstR.width(), dstR.height());
  147. fX = dstR.x();
  148. fY = dstR.y();
  149. return true;
  150. }