SkAndroidCodec.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 SkAndroidCodec_DEFINED
  8. #define SkAndroidCodec_DEFINED
  9. #include "SkCodec.h"
  10. #include "SkEncodedImageFormat.h"
  11. #include "SkStream.h"
  12. #include "SkTypes.h"
  13. /**
  14. * Abstract interface defining image codec functionality that is necessary for
  15. * Android.
  16. */
  17. class SK_API SkAndroidCodec : SkNoncopyable {
  18. public:
  19. enum class ExifOrientationBehavior {
  20. /**
  21. * Ignore any exif orientation markers in the data.
  22. *
  23. * getInfo's width and height will match the header of the image, and
  24. * no processing will be done to match the marker.
  25. */
  26. kIgnore,
  27. /**
  28. * Respect the exif orientation marker.
  29. *
  30. * getInfo's width and height will represent what they should be after
  31. * applying the orientation. For example, if the marker specifies a
  32. * rotation by 90 degrees, they will be swapped relative to the header.
  33. * getAndroidPixels will apply the orientation as well.
  34. */
  35. kRespect,
  36. };
  37. /**
  38. * Pass ownership of an SkCodec to a newly-created SkAndroidCodec.
  39. */
  40. static std::unique_ptr<SkAndroidCodec> MakeFromCodec(std::unique_ptr<SkCodec>,
  41. ExifOrientationBehavior = ExifOrientationBehavior::kIgnore);
  42. /**
  43. * If this stream represents an encoded image that we know how to decode,
  44. * return an SkAndroidCodec that can decode it. Otherwise return NULL.
  45. *
  46. * The SkPngChunkReader handles unknown chunks in PNGs.
  47. * See SkCodec.h for more details.
  48. *
  49. * If NULL is returned, the stream is deleted immediately. Otherwise, the
  50. * SkCodec takes ownership of it, and will delete it when done with it.
  51. *
  52. * ExifOrientationBehavior is set to kIgnore.
  53. */
  54. static std::unique_ptr<SkAndroidCodec> MakeFromStream(std::unique_ptr<SkStream>,
  55. SkPngChunkReader* = nullptr);
  56. /**
  57. * If this data represents an encoded image that we know how to decode,
  58. * return an SkAndroidCodec that can decode it. Otherwise return NULL.
  59. *
  60. * The SkPngChunkReader handles unknown chunks in PNGs.
  61. * See SkCodec.h for more details.
  62. *
  63. * ExifOrientationBehavior is set to kIgnore.
  64. */
  65. static std::unique_ptr<SkAndroidCodec> MakeFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr);
  66. virtual ~SkAndroidCodec();
  67. const SkImageInfo& getInfo() const { return fInfo; }
  68. /**
  69. * Format of the encoded data.
  70. */
  71. SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); }
  72. /**
  73. * @param requestedColorType Color type requested by the client
  74. *
  75. * |requestedColorType| may be overriden. We will default to kF16
  76. * for high precision images.
  77. *
  78. * In the general case, if it is possible to decode to
  79. * |requestedColorType|, this returns |requestedColorType|.
  80. * Otherwise, this returns a color type that is an appropriate
  81. * match for the the encoded data.
  82. */
  83. SkColorType computeOutputColorType(SkColorType requestedColorType);
  84. /**
  85. * @param requestedUnpremul Indicates if the client requested
  86. * unpremultiplied output
  87. *
  88. * Returns the appropriate alpha type to decode to. If the image
  89. * has alpha, the value of requestedUnpremul will be honored.
  90. */
  91. SkAlphaType computeOutputAlphaType(bool requestedUnpremul);
  92. /**
  93. * @param outputColorType Color type that the client will decode to.
  94. * @param prefColorSpace Preferred color space to decode to.
  95. * This may not return |prefColorSpace| for a couple reasons.
  96. * (1) Android Principles: 565 must be sRGB, F16 must be
  97. * linear sRGB, transfer function must be parametric.
  98. * (2) Codec Limitations: F16 requires a linear color space.
  99. *
  100. * Returns the appropriate color space to decode to.
  101. */
  102. sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
  103. sk_sp<SkColorSpace> prefColorSpace = nullptr);
  104. /**
  105. * Compute the appropriate sample size to get to |size|.
  106. *
  107. * @param size As an input parameter, the desired output size of
  108. * the decode. As an output parameter, the smallest sampled size
  109. * larger than the input.
  110. * @return the sample size to set AndroidOptions::fSampleSize to decode
  111. * to the output |size|.
  112. */
  113. int computeSampleSize(SkISize* size) const;
  114. /**
  115. * Returns the dimensions of the scaled output image, for an input
  116. * sampleSize.
  117. *
  118. * When the sample size divides evenly into the original dimensions, the
  119. * scaled output dimensions will simply be equal to the original
  120. * dimensions divided by the sample size.
  121. *
  122. * When the sample size does not divide even into the original
  123. * dimensions, the codec may round up or down, depending on what is most
  124. * efficient to decode.
  125. *
  126. * Finally, the codec will always recommend a non-zero output, so the output
  127. * dimension will always be one if the sampleSize is greater than the
  128. * original dimension.
  129. */
  130. SkISize getSampledDimensions(int sampleSize) const;
  131. /**
  132. * Return (via desiredSubset) a subset which can decoded from this codec,
  133. * or false if the input subset is invalid.
  134. *
  135. * @param desiredSubset in/out parameter
  136. * As input, a desired subset of the original bounds
  137. * (as specified by getInfo).
  138. * As output, if true is returned, desiredSubset may
  139. * have been modified to a subset which is
  140. * supported. Although a particular change may have
  141. * been made to desiredSubset to create something
  142. * supported, it is possible other changes could
  143. * result in a valid subset. If false is returned,
  144. * desiredSubset's value is undefined.
  145. * @return true If the input desiredSubset is valid.
  146. * desiredSubset may be modified to a subset
  147. * supported by the codec.
  148. * false If desiredSubset is invalid (NULL or not fully
  149. * contained within the image).
  150. */
  151. bool getSupportedSubset(SkIRect* desiredSubset) const;
  152. // TODO: Rename SkCodec::getValidSubset() to getSupportedSubset()
  153. /**
  154. * Returns the dimensions of the scaled, partial output image, for an
  155. * input sampleSize and subset.
  156. *
  157. * @param sampleSize Factor to scale down by.
  158. * @param subset Must be a valid subset of the original image
  159. * dimensions and a subset supported by SkAndroidCodec.
  160. * getSubset() can be used to obtain a subset supported
  161. * by SkAndroidCodec.
  162. * @return Size of the scaled partial image. Or zero size
  163. * if either of the inputs is invalid.
  164. */
  165. SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const;
  166. /**
  167. * Additional options to pass to getAndroidPixels().
  168. */
  169. // FIXME: It's a bit redundant to name these AndroidOptions when this class is already
  170. // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
  171. // these Options when SkCodec has a slightly different set of Options. Maybe these
  172. // should be DecodeOptions or SamplingOptions?
  173. struct AndroidOptions {
  174. AndroidOptions()
  175. : fZeroInitialized(SkCodec::kNo_ZeroInitialized)
  176. , fSubset(nullptr)
  177. , fSampleSize(1)
  178. {}
  179. /**
  180. * Indicates is destination pixel memory is zero initialized.
  181. *
  182. * The default is SkCodec::kNo_ZeroInitialized.
  183. */
  184. SkCodec::ZeroInitialized fZeroInitialized;
  185. /**
  186. * If not NULL, represents a subset of the original image to decode.
  187. *
  188. * Must be within the bounds returned by getInfo().
  189. *
  190. * If the EncodedFormat is SkEncodedImageFormat::kWEBP, the top and left
  191. * values must be even.
  192. *
  193. * The default is NULL, meaning a decode of the entire image.
  194. */
  195. SkIRect* fSubset;
  196. /**
  197. * The client may provide an integer downscale factor for the decode.
  198. * The codec may implement this downscaling by sampling or another
  199. * method if it is more efficient.
  200. *
  201. * The default is 1, representing no downscaling.
  202. */
  203. int fSampleSize;
  204. };
  205. /**
  206. * Decode into the given pixels, a block of memory of size at
  207. * least (info.fHeight - 1) * rowBytes + (info.fWidth *
  208. * bytesPerPixel)
  209. *
  210. * Repeated calls to this function should give the same results,
  211. * allowing the PixelRef to be immutable.
  212. *
  213. * @param info A description of the format (config, size)
  214. * expected by the caller. This can simply be identical
  215. * to the info returned by getInfo().
  216. *
  217. * This contract also allows the caller to specify
  218. * different output-configs, which the implementation can
  219. * decide to support or not.
  220. *
  221. * A size that does not match getInfo() implies a request
  222. * to scale or subset. If the codec cannot perform this
  223. * scaling or subsetting, it will return an error code.
  224. *
  225. * The AndroidOptions object is also used to specify any requested scaling or subsetting
  226. * using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above
  227. * for AndroidOptions) are used.
  228. *
  229. * @return Result kSuccess, or another value explaining the type of failure.
  230. */
  231. // FIXME: It's a bit redundant to name this getAndroidPixels() when this class is already
  232. // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
  233. // this getPixels() when it is a slightly different API than SkCodec's getPixels().
  234. // Maybe this should be decode() or decodeSubset()?
  235. SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
  236. const AndroidOptions* options);
  237. /**
  238. * Simplified version of getAndroidPixels() where we supply the default AndroidOptions as
  239. * specified above for AndroidOptions. It will not perform any scaling or subsetting.
  240. */
  241. SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
  242. SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
  243. return this->getAndroidPixels(info, pixels, rowBytes);
  244. }
  245. SkCodec* codec() const { return fCodec.get(); }
  246. protected:
  247. SkAndroidCodec(SkCodec*, ExifOrientationBehavior = ExifOrientationBehavior::kIgnore);
  248. virtual SkISize onGetSampledDimensions(int sampleSize) const = 0;
  249. virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0;
  250. virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels,
  251. size_t rowBytes, const AndroidOptions& options) = 0;
  252. private:
  253. const SkImageInfo fInfo;
  254. const ExifOrientationBehavior fOrientationBehavior;
  255. std::unique_ptr<SkCodec> fCodec;
  256. };
  257. #endif // SkAndroidCodec_DEFINED