skia_utils_base.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "skia/ext/skia_utils_base.h"
  5. #include <stdint.h>
  6. #include "base/pickle.h"
  7. #include "base/strings/stringprintf.h"
  8. #include "third_party/skia/include/core/SkBitmap.h"
  9. #include "third_party/skia/include/core/SkData.h"
  10. #include "third_party/skia/include/core/SkEncodedImageFormat.h"
  11. #include "third_party/skia/include/core/SkImage.h"
  12. #include "third_party/skia/include/core/SkImageInfo.h"
  13. #include "third_party/skia/include/core/SkSerialProcs.h"
  14. namespace skia {
  15. bool ReadSkString(base::PickleIterator* iter, SkString* str) {
  16. size_t reply_length;
  17. const char* reply_text;
  18. if (!iter->ReadData(&reply_text, &reply_length))
  19. return false;
  20. if (str)
  21. str->set(reply_text, reply_length);
  22. return true;
  23. }
  24. bool ReadSkFontIdentity(base::PickleIterator* iter,
  25. SkFontConfigInterface::FontIdentity* identity) {
  26. uint32_t reply_id;
  27. uint32_t reply_ttcIndex;
  28. size_t reply_length;
  29. const char* reply_text;
  30. if (!iter->ReadUInt32(&reply_id) ||
  31. !iter->ReadUInt32(&reply_ttcIndex) ||
  32. !iter->ReadData(&reply_text, &reply_length))
  33. return false;
  34. if (identity) {
  35. identity->fID = reply_id;
  36. identity->fTTCIndex = reply_ttcIndex;
  37. identity->fString.set(reply_text, reply_length);
  38. }
  39. return true;
  40. }
  41. bool ReadSkFontStyle(base::PickleIterator* iter, SkFontStyle* style) {
  42. uint16_t reply_weight;
  43. uint16_t reply_width;
  44. uint16_t reply_slant;
  45. if (!iter->ReadUInt16(&reply_weight) ||
  46. !iter->ReadUInt16(&reply_width) ||
  47. !iter->ReadUInt16(&reply_slant))
  48. return false;
  49. if (style) {
  50. *style = SkFontStyle(reply_weight,
  51. reply_width,
  52. static_cast<SkFontStyle::Slant>(reply_slant));
  53. }
  54. return true;
  55. }
  56. void WriteSkString(base::Pickle* pickle, const SkString& str) {
  57. pickle->WriteData(str.c_str(), str.size());
  58. }
  59. void WriteSkFontIdentity(base::Pickle* pickle,
  60. const SkFontConfigInterface::FontIdentity& identity) {
  61. pickle->WriteUInt32(identity.fID);
  62. pickle->WriteUInt32(identity.fTTCIndex);
  63. WriteSkString(pickle, identity.fString);
  64. }
  65. void WriteSkFontStyle(base::Pickle* pickle, SkFontStyle style) {
  66. pickle->WriteUInt16(style.weight());
  67. pickle->WriteUInt16(style.width());
  68. pickle->WriteUInt16(style.slant());
  69. }
  70. bool SkBitmapToN32OpaqueOrPremul(const SkBitmap& in, SkBitmap* out) {
  71. DCHECK(out);
  72. if (in.colorType() == kUnknown_SkColorType &&
  73. in.alphaType() == kUnknown_SkAlphaType && in.empty() && in.isNull()) {
  74. // Default-initialized bitmaps convert to the same.
  75. *out = SkBitmap();
  76. return true;
  77. }
  78. const SkImageInfo& info = in.info();
  79. const bool stride_matches_width = in.rowBytes() == info.minRowBytes();
  80. if (stride_matches_width && info.colorType() == kN32_SkColorType &&
  81. (info.alphaType() == kPremul_SkAlphaType ||
  82. info.alphaType() == kOpaque_SkAlphaType)) {
  83. // Shallow copy if the data is already in the right format.
  84. *out = in;
  85. return true;
  86. }
  87. SkImageInfo new_info =
  88. info.makeColorType(kN32_SkColorType)
  89. .makeAlphaType(info.alphaType() == kOpaque_SkAlphaType
  90. ? kOpaque_SkAlphaType
  91. : kPremul_SkAlphaType);
  92. if (!out->tryAllocPixels(new_info, 0)) {
  93. return false;
  94. }
  95. if (!in.readPixels(out->pixmap())) {
  96. return false;
  97. }
  98. return true;
  99. }
  100. std::string SkColorToHexString(SkColor color) {
  101. return base::StringPrintf("#%02X%02X%02X", SkColorGetR(color),
  102. SkColorGetG(color), SkColorGetB(color));
  103. }
  104. } // namespace skia