sk_imageinfo.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2018 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/core/SkColorSpace.h"
  8. #include "include/core/SkImageInfo.h"
  9. #include "include/c/sk_colorspace.h"
  10. #include "include/c/sk_imageinfo.h"
  11. const struct {
  12. sk_colortype_t fC;
  13. SkColorType fSK;
  14. } gColorTypeMap[] = {
  15. { UNKNOWN_SK_COLORTYPE, kUnknown_SkColorType },
  16. { RGBA_8888_SK_COLORTYPE, kRGBA_8888_SkColorType },
  17. { BGRA_8888_SK_COLORTYPE, kBGRA_8888_SkColorType },
  18. { ALPHA_8_SK_COLORTYPE, kAlpha_8_SkColorType },
  19. { GRAY_8_SK_COLORTYPE, kGray_8_SkColorType },
  20. { RGBA_F16_SK_COLORTYPE, kRGBA_F16_SkColorType },
  21. { RGBA_F32_SK_COLORTYPE, kRGBA_F32_SkColorType },
  22. };
  23. const struct {
  24. sk_alphatype_t fC;
  25. SkAlphaType fSK;
  26. } gAlphaTypeMap[] = {
  27. { OPAQUE_SK_ALPHATYPE, kOpaque_SkAlphaType },
  28. { PREMUL_SK_ALPHATYPE, kPremul_SkAlphaType },
  29. { UNPREMUL_SK_ALPHATYPE, kUnpremul_SkAlphaType },
  30. };
  31. static bool from_c_colortype(sk_colortype_t cCT, SkColorType* skCT) {
  32. for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) {
  33. if (gColorTypeMap[i].fC == cCT) {
  34. if (skCT) {
  35. *skCT = gColorTypeMap[i].fSK;
  36. }
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. static bool to_c_colortype(SkColorType skCT, sk_colortype_t* cCT) {
  43. for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) {
  44. if (gColorTypeMap[i].fSK == skCT) {
  45. if (cCT) {
  46. *cCT = gColorTypeMap[i].fC;
  47. }
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53. static bool from_c_alphatype(sk_alphatype_t cAT, SkAlphaType* skAT) {
  54. for (size_t i = 0; i < SK_ARRAY_COUNT(gAlphaTypeMap); ++i) {
  55. if (gAlphaTypeMap[i].fC == cAT) {
  56. if (skAT) {
  57. *skAT = gAlphaTypeMap[i].fSK;
  58. }
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. static bool to_c_alphatype(SkAlphaType skAT, sk_alphatype_t* cAT) {
  65. for (size_t i = 0; i < SK_ARRAY_COUNT(gAlphaTypeMap); ++i) {
  66. if (gAlphaTypeMap[i].fSK == skAT) {
  67. if (cAT) {
  68. *cAT = gAlphaTypeMap[i].fC;
  69. }
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. const SkImageInfo* ToImageInfo(const sk_imageinfo_t* cinfo) {
  76. return reinterpret_cast<const SkImageInfo*>(cinfo);
  77. }
  78. /////////////////////////////////////////////////////////////////////////////////////////////
  79. sk_imageinfo_t* sk_imageinfo_new(int w, int h, sk_colortype_t cct, sk_alphatype_t cat,
  80. sk_colorspace_t* ccs) {
  81. SkColorType ct;
  82. SkAlphaType at;
  83. if (!from_c_colortype(cct, &ct) || !from_c_alphatype(cat, &at)) {
  84. return nullptr;
  85. }
  86. SkColorSpace* cs = (SkColorSpace*)ccs;
  87. SkImageInfo* info = new SkImageInfo(SkImageInfo::Make(w, h, ct, at, sk_ref_sp(cs)));
  88. return reinterpret_cast<sk_imageinfo_t*>(info);
  89. }
  90. void sk_imageinfo_delete(sk_imageinfo_t* cinfo) {
  91. delete ToImageInfo(cinfo);
  92. }
  93. int32_t sk_imageinfo_get_width(const sk_imageinfo_t* cinfo) {
  94. return ToImageInfo(cinfo)->width();
  95. }
  96. int32_t sk_imageinfo_get_height(const sk_imageinfo_t* cinfo) {
  97. return ToImageInfo(cinfo)->height();
  98. }
  99. sk_colortype_t sk_imageinfo_get_colortype(const sk_imageinfo_t* cinfo) {
  100. sk_colortype_t ct;
  101. return to_c_colortype(ToImageInfo(cinfo)->colorType(), &ct) ? ct : UNKNOWN_SK_COLORTYPE;
  102. }
  103. sk_alphatype_t sk_imageinfo_get_alphatype(const sk_imageinfo_t* cinfo) {
  104. sk_alphatype_t at;
  105. // odd that we return premul on failure...
  106. return to_c_alphatype(ToImageInfo(cinfo)->alphaType(), &at) ? at : PREMUL_SK_ALPHATYPE;
  107. }
  108. sk_colorspace_t* sk_imageinfo_get_colorspace(const sk_imageinfo_t* cinfo) {
  109. return reinterpret_cast<sk_colorspace_t*>(ToImageInfo(cinfo)->colorSpace());
  110. }
  111. /////////////////////////////////////////////////////////////////////////////////////////////
  112. sk_colorspace_t* sk_colorspace_new_srgb() {
  113. return reinterpret_cast<sk_colorspace_t*>(SkColorSpace::MakeSRGB().release());
  114. }
  115. void sk_colorspace_ref(sk_colorspace_t* cs) {
  116. SkSafeRef(reinterpret_cast<SkColorSpace*>(cs));
  117. }
  118. void sk_colorspace_unref(sk_colorspace_t* cs) {
  119. SkSafeUnref(reinterpret_cast<SkColorSpace*>(cs));
  120. }