blob: 160c7f9ab7099d9654738cfb7bd26f9929f8f2a4 [file] [log] [blame]
Mike Reed66c505f2018-09-28 11:27:57 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkColorSpace.h"
9#include "include/core/SkImageInfo.h"
Mike Reed66c505f2018-09-28 11:27:57 -040010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/c/sk_colorspace.h"
12#include "include/c/sk_imageinfo.h"
Mike Reed66c505f2018-09-28 11:27:57 -040013
14const struct {
15 sk_colortype_t fC;
16 SkColorType fSK;
17} gColorTypeMap[] = {
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 },
25};
26
27const struct {
28 sk_alphatype_t fC;
29 SkAlphaType fSK;
30} gAlphaTypeMap[] = {
31 { OPAQUE_SK_ALPHATYPE, kOpaque_SkAlphaType },
32 { PREMUL_SK_ALPHATYPE, kPremul_SkAlphaType },
33 { UNPREMUL_SK_ALPHATYPE, kUnpremul_SkAlphaType },
34};
35
36static 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) {
39 if (skCT) {
40 *skCT = gColorTypeMap[i].fSK;
41 }
42 return true;
43 }
44 }
45 return false;
46}
47
48static 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) {
51 if (cCT) {
52 *cCT = gColorTypeMap[i].fC;
53 }
54 return true;
55 }
56 }
57 return false;
58}
59
60static 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) {
63 if (skAT) {
64 *skAT = gAlphaTypeMap[i].fSK;
65 }
66 return true;
67 }
68 }
69 return false;
70}
71
72static 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) {
75 if (cAT) {
76 *cAT = gAlphaTypeMap[i].fC;
77 }
78 return true;
79 }
80 }
81 return false;
82}
83
84const SkImageInfo* ToImageInfo(const sk_imageinfo_t* cinfo) {
85 return reinterpret_cast<const SkImageInfo*>(cinfo);
86}
87
88/////////////////////////////////////////////////////////////////////////////////////////////
89
90sk_imageinfo_t* sk_imageinfo_new(int w, int h, sk_colortype_t cct, sk_alphatype_t cat,
91 sk_colorspace_t* ccs) {
92 SkColorType ct;
93 SkAlphaType at;
94 if (!from_c_colortype(cct, &ct) || !from_c_alphatype(cat, &at)) {
95 return nullptr;
96 }
97 SkColorSpace* cs = (SkColorSpace*)ccs;
98
99 SkImageInfo* info = new SkImageInfo(SkImageInfo::Make(w, h, ct, at, sk_ref_sp(cs)));
100 return reinterpret_cast<sk_imageinfo_t*>(info);
101}
102
103void sk_imageinfo_delete(sk_imageinfo_t* cinfo) {
104 delete ToImageInfo(cinfo);
105}
106
Stan Iliev82310312019-06-05 16:55:54 -0400107int32_t sk_imageinfo_get_width(const sk_imageinfo_t* cinfo) {
Mike Reed66c505f2018-09-28 11:27:57 -0400108 return ToImageInfo(cinfo)->width();
109}
110
Stan Iliev82310312019-06-05 16:55:54 -0400111int32_t sk_imageinfo_get_height(const sk_imageinfo_t* cinfo) {
Mike Reed66c505f2018-09-28 11:27:57 -0400112 return ToImageInfo(cinfo)->height();
113}
114
115sk_colortype_t sk_imageinfo_get_colortype(const sk_imageinfo_t* cinfo) {
116 sk_colortype_t ct;
117 return to_c_colortype(ToImageInfo(cinfo)->colorType(), &ct) ? ct : UNKNOWN_SK_COLORTYPE;
118}
119
120sk_alphatype_t sk_imageinfo_get_alphatype(const sk_imageinfo_t* cinfo) {
121 sk_alphatype_t at;
122 // odd that we return premul on failure...
123 return to_c_alphatype(ToImageInfo(cinfo)->alphaType(), &at) ? at : PREMUL_SK_ALPHATYPE;
124}
125
126sk_colorspace_t* sk_imageinfo_get_colorspace(const sk_imageinfo_t* cinfo) {
127 return reinterpret_cast<sk_colorspace_t*>(ToImageInfo(cinfo)->colorSpace());
128}
129
130/////////////////////////////////////////////////////////////////////////////////////////////
131
132sk_colorspace_t* sk_colorspace_new_srgb() {
133 return reinterpret_cast<sk_colorspace_t*>(SkColorSpace::MakeSRGB().release());
134}
135
136void sk_colorspace_ref(sk_colorspace_t* cs) {
137 SkSafeRef(reinterpret_cast<SkColorSpace*>(cs));
138}
139
140void sk_colorspace_unref(sk_colorspace_t* cs) {
141 SkSafeUnref(reinterpret_cast<SkColorSpace*>(cs));
142}
143