SkCodecPriv.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright 2015 The Android Open Source Project
  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 SkCodecPriv_DEFINED
  8. #define SkCodecPriv_DEFINED
  9. #include "include/codec/SkEncodedOrigin.h"
  10. #include "include/core/SkImageInfo.h"
  11. #include "include/core/SkTypes.h"
  12. #include "include/private/SkColorData.h"
  13. #include "include/private/SkEncodedInfo.h"
  14. #include "src/codec/SkColorTable.h"
  15. #ifdef SK_PRINT_CODEC_MESSAGES
  16. #define SkCodecPrintf SkDebugf
  17. #else
  18. #define SkCodecPrintf(...)
  19. #endif
  20. // Defined in SkCodec.cpp
  21. bool sk_select_xform_format(SkColorType colorType, bool forColorTable,
  22. skcms_PixelFormat* outFormat);
  23. // FIXME: Consider sharing with dm, nanbench, and tools.
  24. static inline float get_scale_from_sample_size(int sampleSize) {
  25. return 1.0f / ((float) sampleSize);
  26. }
  27. static inline bool is_valid_subset(const SkIRect& subset, const SkISize& imageDims) {
  28. return SkIRect::MakeSize(imageDims).contains(subset);
  29. }
  30. /*
  31. * returns a scaled dimension based on the original dimension and the sampleSize
  32. * NOTE: we round down here for scaled dimension to match the behavior of SkImageDecoder
  33. * FIXME: I think we should call this get_sampled_dimension().
  34. */
  35. static inline int get_scaled_dimension(int srcDimension, int sampleSize) {
  36. if (sampleSize > srcDimension) {
  37. return 1;
  38. }
  39. return srcDimension / sampleSize;
  40. }
  41. /*
  42. * Returns the first coordinate that we will keep during a scaled decode.
  43. * The output can be interpreted as an x-coordinate or a y-coordinate.
  44. *
  45. * This does not need to be called and is not called when sampleFactor == 1.
  46. */
  47. static inline int get_start_coord(int sampleFactor) { return sampleFactor / 2; };
  48. /*
  49. * Given a coordinate in the original image, this returns the corresponding
  50. * coordinate in the scaled image. This function is meaningless if
  51. * IsCoordNecessary returns false.
  52. * The output can be interpreted as an x-coordinate or a y-coordinate.
  53. *
  54. * This does not need to be called and is not called when sampleFactor == 1.
  55. */
  56. static inline int get_dst_coord(int srcCoord, int sampleFactor) { return srcCoord / sampleFactor; };
  57. /*
  58. * When scaling, we will discard certain y-coordinates (rows) and
  59. * x-coordinates (columns). This function returns true if we should keep the
  60. * coordinate and false otherwise.
  61. * The inputs may be x-coordinates or y-coordinates.
  62. *
  63. * This does not need to be called and is not called when sampleFactor == 1.
  64. */
  65. static inline bool is_coord_necessary(int srcCoord, int sampleFactor, int scaledDim) {
  66. // Get the first coordinate that we want to keep
  67. int startCoord = get_start_coord(sampleFactor);
  68. // Return false on edge cases
  69. if (srcCoord < startCoord || get_dst_coord(srcCoord, sampleFactor) >= scaledDim) {
  70. return false;
  71. }
  72. // Every sampleFactor rows are necessary
  73. return ((srcCoord - startCoord) % sampleFactor) == 0;
  74. }
  75. static inline bool valid_alpha(SkAlphaType dstAlpha, bool srcIsOpaque) {
  76. if (kUnknown_SkAlphaType == dstAlpha) {
  77. return false;
  78. }
  79. if (srcIsOpaque) {
  80. if (kOpaque_SkAlphaType != dstAlpha) {
  81. SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
  82. "- it is being decoded as non-opaque, which will draw slower\n");
  83. }
  84. return true;
  85. }
  86. return dstAlpha != kOpaque_SkAlphaType;
  87. }
  88. /*
  89. * If there is a color table, get a pointer to the colors, otherwise return nullptr
  90. */
  91. static inline const SkPMColor* get_color_ptr(SkColorTable* colorTable) {
  92. return nullptr != colorTable ? colorTable->readColors() : nullptr;
  93. }
  94. /*
  95. * Compute row bytes for an image using pixels per byte
  96. */
  97. static inline size_t compute_row_bytes_ppb(int width, uint32_t pixelsPerByte) {
  98. return (width + pixelsPerByte - 1) / pixelsPerByte;
  99. }
  100. /*
  101. * Compute row bytes for an image using bytes per pixel
  102. */
  103. static inline size_t compute_row_bytes_bpp(int width, uint32_t bytesPerPixel) {
  104. return width * bytesPerPixel;
  105. }
  106. /*
  107. * Compute row bytes for an image
  108. */
  109. static inline size_t compute_row_bytes(int width, uint32_t bitsPerPixel) {
  110. if (bitsPerPixel < 16) {
  111. SkASSERT(0 == 8 % bitsPerPixel);
  112. const uint32_t pixelsPerByte = 8 / bitsPerPixel;
  113. return compute_row_bytes_ppb(width, pixelsPerByte);
  114. } else {
  115. SkASSERT(0 == bitsPerPixel % 8);
  116. const uint32_t bytesPerPixel = bitsPerPixel / 8;
  117. return compute_row_bytes_bpp(width, bytesPerPixel);
  118. }
  119. }
  120. /*
  121. * Get a byte from a buffer
  122. * This method is unsafe, the caller is responsible for performing a check
  123. */
  124. static inline uint8_t get_byte(uint8_t* buffer, uint32_t i) {
  125. return buffer[i];
  126. }
  127. /*
  128. * Get a short from a buffer
  129. * This method is unsafe, the caller is responsible for performing a check
  130. */
  131. static inline uint16_t get_short(uint8_t* buffer, uint32_t i) {
  132. uint16_t result;
  133. memcpy(&result, &(buffer[i]), 2);
  134. #ifdef SK_CPU_BENDIAN
  135. return SkEndianSwap16(result);
  136. #else
  137. return result;
  138. #endif
  139. }
  140. /*
  141. * Get an int from a buffer
  142. * This method is unsafe, the caller is responsible for performing a check
  143. */
  144. static inline uint32_t get_int(uint8_t* buffer, uint32_t i) {
  145. uint32_t result;
  146. memcpy(&result, &(buffer[i]), 4);
  147. #ifdef SK_CPU_BENDIAN
  148. return SkEndianSwap32(result);
  149. #else
  150. return result;
  151. #endif
  152. }
  153. /*
  154. * @param data Buffer to read bytes from
  155. * @param isLittleEndian Output parameter
  156. * Indicates if the data is little endian
  157. * Is unaffected on false returns
  158. */
  159. static inline bool is_valid_endian_marker(const uint8_t* data, bool* isLittleEndian) {
  160. // II indicates Intel (little endian) and MM indicates motorola (big endian).
  161. if (('I' != data[0] || 'I' != data[1]) && ('M' != data[0] || 'M' != data[1])) {
  162. return false;
  163. }
  164. *isLittleEndian = ('I' == data[0]);
  165. return true;
  166. }
  167. static inline uint16_t get_endian_short(const uint8_t* data, bool littleEndian) {
  168. if (littleEndian) {
  169. return (data[1] << 8) | (data[0]);
  170. }
  171. return (data[0] << 8) | (data[1]);
  172. }
  173. static inline SkPMColor premultiply_argb_as_rgba(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
  174. if (a != 255) {
  175. r = SkMulDiv255Round(r, a);
  176. g = SkMulDiv255Round(g, a);
  177. b = SkMulDiv255Round(b, a);
  178. }
  179. return SkPackARGB_as_RGBA(a, r, g, b);
  180. }
  181. static inline SkPMColor premultiply_argb_as_bgra(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
  182. if (a != 255) {
  183. r = SkMulDiv255Round(r, a);
  184. g = SkMulDiv255Round(g, a);
  185. b = SkMulDiv255Round(b, a);
  186. }
  187. return SkPackARGB_as_BGRA(a, r, g, b);
  188. }
  189. static inline bool is_rgba(SkColorType colorType) {
  190. #ifdef SK_PMCOLOR_IS_RGBA
  191. return (kBGRA_8888_SkColorType != colorType);
  192. #else
  193. return (kRGBA_8888_SkColorType == colorType);
  194. #endif
  195. }
  196. // Method for coverting to a 32 bit pixel.
  197. typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
  198. static inline PackColorProc choose_pack_color_proc(bool isPremul, SkColorType colorType) {
  199. bool isRGBA = is_rgba(colorType);
  200. if (isPremul) {
  201. if (isRGBA) {
  202. return &premultiply_argb_as_rgba;
  203. } else {
  204. return &premultiply_argb_as_bgra;
  205. }
  206. } else {
  207. if (isRGBA) {
  208. return &SkPackARGB_as_RGBA;
  209. } else {
  210. return &SkPackARGB_as_BGRA;
  211. }
  212. }
  213. }
  214. bool is_orientation_marker(const uint8_t* data, size_t data_length, SkEncodedOrigin* orientation);
  215. #endif // SkCodecPriv_DEFINED