2 * Copyright 2018 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/core/SkColorSpace.h"
9 #include "include/core/SkImageInfo.h"
11 #include "include/c/sk_colorspace.h"
12 #include "include/c/sk_imageinfo.h"
18 { UNKNOWN_SK_COLORTYPE, kUnknown_SkColorType },
19 { RGBA_8888_SK_COLORTYPE, kRGBA_8888_SkColorType },
20 { BGRA_8888_SK_COLORTYPE, kBGRA_8888_SkColorType },
21 { ALPHA_8_SK_COLORTYPE, kAlpha_8_SkColorType },
22 { GRAY_8_SK_COLORTYPE, kGray_8_SkColorType },
23 { RGBA_F16_SK_COLORTYPE, kRGBA_F16_SkColorType },
24 { RGBA_F32_SK_COLORTYPE, kRGBA_F32_SkColorType },
31 { OPAQUE_SK_ALPHATYPE, kOpaque_SkAlphaType },
32 { PREMUL_SK_ALPHATYPE, kPremul_SkAlphaType },
33 { UNPREMUL_SK_ALPHATYPE, kUnpremul_SkAlphaType },
36 static bool from_c_colortype(sk_colortype_t cCT, SkColorType* skCT) {
37 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) {
38 if (gColorTypeMap[i].fC == cCT) {
40 *skCT = gColorTypeMap[i].fSK;
48 static bool to_c_colortype(SkColorType skCT, sk_colortype_t* cCT) {
49 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) {
50 if (gColorTypeMap[i].fSK == skCT) {
52 *cCT = gColorTypeMap[i].fC;
60 static bool from_c_alphatype(sk_alphatype_t cAT, SkAlphaType* skAT) {
61 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlphaTypeMap); ++i) {
62 if (gAlphaTypeMap[i].fC == cAT) {
64 *skAT = gAlphaTypeMap[i].fSK;
72 static bool to_c_alphatype(SkAlphaType skAT, sk_alphatype_t* cAT) {
73 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlphaTypeMap); ++i) {
74 if (gAlphaTypeMap[i].fSK == skAT) {
76 *cAT = gAlphaTypeMap[i].fC;
84 const SkImageInfo* ToImageInfo(const sk_imageinfo_t* cinfo) {
85 return reinterpret_cast<const SkImageInfo*>(cinfo);
88 /////////////////////////////////////////////////////////////////////////////////////////////
90 sk_imageinfo_t* sk_imageinfo_new(int w, int h, sk_colortype_t cct, sk_alphatype_t cat,
91 sk_colorspace_t* ccs) {
94 if (!from_c_colortype(cct, &ct) || !from_c_alphatype(cat, &at)) {
97 SkColorSpace* cs = (SkColorSpace*)ccs;
99 SkImageInfo* info = new SkImageInfo(SkImageInfo::Make(w, h, ct, at, sk_ref_sp(cs)));
100 return reinterpret_cast<sk_imageinfo_t*>(info);
103 void sk_imageinfo_delete(sk_imageinfo_t* cinfo) {
104 delete ToImageInfo(cinfo);
107 int32_t sk_imageinfo_get_width(const sk_imageinfo_t* cinfo) {
108 return ToImageInfo(cinfo)->width();
111 int32_t sk_imageinfo_get_height(const sk_imageinfo_t* cinfo) {
112 return ToImageInfo(cinfo)->height();
115 sk_colortype_t sk_imageinfo_get_colortype(const sk_imageinfo_t* cinfo) {
117 return to_c_colortype(ToImageInfo(cinfo)->colorType(), &ct) ? ct : UNKNOWN_SK_COLORTYPE;
120 sk_alphatype_t sk_imageinfo_get_alphatype(const sk_imageinfo_t* cinfo) {
122 // odd that we return premul on failure...
123 return to_c_alphatype(ToImageInfo(cinfo)->alphaType(), &at) ? at : PREMUL_SK_ALPHATYPE;
126 sk_colorspace_t* sk_imageinfo_get_colorspace(const sk_imageinfo_t* cinfo) {
127 return reinterpret_cast<sk_colorspace_t*>(ToImageInfo(cinfo)->colorSpace());
130 /////////////////////////////////////////////////////////////////////////////////////////////
132 sk_colorspace_t* sk_colorspace_new_srgb() {
133 return reinterpret_cast<sk_colorspace_t*>(SkColorSpace::MakeSRGB().release());
136 void sk_colorspace_ref(sk_colorspace_t* cs) {
137 SkSafeRef(reinterpret_cast<SkColorSpace*>(cs));
140 void sk_colorspace_unref(sk_colorspace_t* cs) {
141 SkSafeUnref(reinterpret_cast<SkColorSpace*>(cs));