Index: src/c/sk_surface.cpp |
diff --git a/experimental/sk_surface.cpp b/src/c/sk_surface.cpp |
similarity index 70% |
rename from experimental/sk_surface.cpp |
rename to src/c/sk_surface.cpp |
index ae37c464b2aba1e135b9f7170578b6c4779c1ec4..ec16fc27fbd7054fbe41ef014839f7af98dc0108 100644 |
--- a/experimental/sk_surface.cpp |
+++ b/src/c/sk_surface.cpp |
@@ -14,9 +14,77 @@ |
#include "SkPath.h" |
#include "SkSurface.h" |
-static SkImageInfo make(const sk_imageinfo_t& cinfo) { |
- return SkImageInfo::Make(cinfo.width, cinfo.height, |
- (SkColorType)cinfo.colorType, (SkAlphaType)cinfo.alphaType); |
+const struct { |
+ sk_colortype_t fC; |
+ SkColorType fSK; |
+} gColorTypeMap[] = { |
+ { UNKNOWN_SK_COLORTYPE, kUnknown_SkColorType }, |
+ { RGBA_8888_SK_COLORTYPE, kRGBA_8888_SkColorType }, |
+ { BGRA_8888_SK_COLORTYPE, kBGRA_8888_SkColorType }, |
+ { ALPHA_8_SK_COLORTYPE, kAlpha_8_SkColorType }, |
+}; |
+ |
+const struct { |
+ sk_alphatype_t fC; |
+ SkAlphaType fSK; |
+} gAlphaTypeMap[] = { |
+ { OPAQUE_SK_ALPHATYPE, kOpaque_SkAlphaType }, |
+ { PREMUL_SK_ALPHATYPE, kPremul_SkAlphaType }, |
+ { UNPREMUL_SK_ALPHATYPE, kUnpremul_SkAlphaType }, |
+}; |
+ |
+static bool from_c_colortype(sk_colortype_t cCT, SkColorType* skCT) { |
+ for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) { |
+ if (gColorTypeMap[i].fC == cCT) { |
+ if (skCT) { |
+ *skCT = gColorTypeMap[i].fSK; |
+ } |
+ return true; |
+ } |
+ } |
+ return false; |
+} |
+ |
+static bool to_c_colortype(SkColorType skCT, sk_colortype_t* cCT) { |
+ for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) { |
+ if (gColorTypeMap[i].fSK == skCT) { |
+ if (cCT) { |
+ *cCT = gColorTypeMap[i].fC; |
+ } |
+ return true; |
+ } |
+ } |
+ return false; |
+} |
+ |
+static bool from_c_alphatype(sk_alphatype_t cAT, SkAlphaType* skAT) { |
+ for (size_t i = 0; i < SK_ARRAY_COUNT(gAlphaTypeMap); ++i) { |
+ if (gAlphaTypeMap[i].fC == cAT) { |
+ if (skAT) { |
+ *skAT = gAlphaTypeMap[i].fSK; |
+ } |
+ return true; |
+ } |
+ } |
+ return false; |
+} |
+ |
+static bool from_c_info(const sk_imageinfo_t& cinfo, SkImageInfo* info) { |
+ SkColorType ct; |
+ SkAlphaType at; |
+ |
+ if (!from_c_colortype(cinfo.colorType, &ct)) { |
+ // optionally report error to client? |
+ return false; |
+ } |
+ if (!from_c_alphatype(cinfo.alphaType, &at)) { |
+ // optionally report error to client? |
+ return false; |
+ } |
+ if (info) { |
+ *info = SkImageInfo::Make(cinfo.width, cinfo.height, ct, at); |
+ } |
+ return true; |
} |
static const SkRect& AsRect(const sk_rect_t& crect) { |
@@ -53,9 +121,23 @@ static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) { |
/////////////////////////////////////////////////////////////////////////////////////////// |
+sk_colortype_t sk_colortype_get_default_8888() { |
+ sk_colortype_t ct; |
+ if (!to_c_colortype(kN32_SkColorType, &ct)) { |
+ ct = UNKNOWN_SK_COLORTYPE; |
+ } |
+ return ct; |
+} |
+ |
+/////////////////////////////////////////////////////////////////////////////////////////// |
+ |
sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels, |
size_t rowBytes) { |
- return (sk_image_t*)SkImage::NewRasterCopy(make(*cinfo), pixels, rowBytes); |
+ SkImageInfo info; |
+ if (!from_c_info(*cinfo, &info)) { |
+ return NULL; |
+ } |
+ return (sk_image_t*)SkImage::NewRasterCopy(info, pixels, rowBytes); |
} |
void sk_image_ref(const sk_image_t* cimage) { |
@@ -176,12 +258,20 @@ void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float |
/////////////////////////////////////////////////////////////////////////////////////////// |
sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo) { |
- return (sk_surface_t*)SkSurface::NewRaster(make(*cinfo)); |
+ SkImageInfo info; |
+ if (!from_c_info(*cinfo, &info)) { |
+ return NULL; |
+ } |
+ return (sk_surface_t*)SkSurface::NewRaster(info); |
} |
sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels, |
size_t rowBytes) { |
- return (sk_surface_t*)SkSurface::NewRasterDirect(make(*cinfo), pixels, rowBytes); |
+ SkImageInfo info; |
+ if (!from_c_info(*cinfo, &info)) { |
+ return NULL; |
+ } |
+ return (sk_surface_t*)SkSurface::NewRasterDirect(info, pixels, rowBytes); |
} |
void sk_surface_delete(sk_surface_t* csurf) { |