Added a member to swizzle the R and B
[platform/upstream/libSkiaSharp.git] / src / c / sk_pixmap.cpp
1 /*
2  * Copyright 2016 Xamarin 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
8 #include "SkPixmap.h"
9 #include "SkBitmapScaler.h"
10 #include "SkImageEncoder.h"
11 #include "SkSwizzle.h"
12
13 #include "sk_pixmap.h"
14
15 #include "sk_types_priv.h"
16
17
18 void sk_pixmap_destructor(sk_pixmap_t* cpixmap)
19 {
20     delete AsPixmap(cpixmap);
21 }
22
23 sk_pixmap_t* sk_pixmap_new()
24 {
25     return ToPixmap(new SkPixmap());
26 }
27
28 sk_pixmap_t* sk_pixmap_new_with_params(const sk_imageinfo_t* cinfo, const void* addr, size_t rowBytes, sk_colortable_t* ctable)
29 {
30     SkImageInfo info;
31     from_c(*cinfo, &info);
32
33     return ToPixmap(new SkPixmap(info, addr, rowBytes, AsColorTable(ctable)));
34 }
35
36 void sk_pixmap_reset(sk_pixmap_t* cpixmap)
37 {
38     AsPixmap(cpixmap)->reset();
39 }
40
41 void sk_pixmap_reset_with_params(sk_pixmap_t* cpixmap, const sk_imageinfo_t* cinfo, const void* addr, size_t rowBytes, sk_colortable_t* ctable)
42 {
43     SkImageInfo info;
44     from_c(*cinfo, &info);
45
46     AsPixmap(cpixmap)->reset(info, addr, rowBytes, AsColorTable(ctable));
47 }
48
49 void sk_pixmap_get_info(sk_pixmap_t* cpixmap, sk_imageinfo_t* cinfo)
50 {
51     from_sk(AsPixmap(cpixmap)->info(), cinfo);
52 }
53
54 size_t sk_pixmap_get_row_bytes(sk_pixmap_t* cpixmap)
55 {
56     return AsPixmap(cpixmap)->rowBytes();
57 }
58
59 const void* sk_pixmap_get_pixels(sk_pixmap_t* cpixmap)
60 {
61     return AsPixmap(cpixmap)->addr();
62 }
63
64 sk_colortable_t* sk_pixmap_get_colortable(sk_pixmap_t* cpixmap)
65 {
66     return ToColorTable(AsPixmap(cpixmap)->ctable());
67 }
68
69 bool sk_bitmapscaler_resize(const sk_pixmap_t* cdst, const sk_pixmap_t* csrc, sk_bitmapscaler_resizemethod_t method)
70 {
71     const SkPixmap& dst = AsPixmap(*cdst);
72     const SkPixmap& src = AsPixmap(*csrc);
73     return SkBitmapScaler::Resize(dst, src, (SkBitmapScaler::ResizeMethod)method);
74 }
75
76 sk_color_t sk_color_unpremultiply(const sk_pmcolor_t pmcolor)
77 {
78     return SkUnPreMultiply::PMColorToColor(pmcolor);
79 }
80
81 sk_pmcolor_t sk_color_premultiply(const sk_color_t color)
82 {
83     return SkPreMultiplyColor(color);
84 }
85
86 void sk_color_unpremultiply_array(const sk_pmcolor_t* pmcolors, int size, sk_color_t* colors)
87 {
88     for (int i = 0; i < size; ++i) {
89         colors[i] = SkUnPreMultiply::PMColorToColor(pmcolors[i]);
90     }
91 }
92
93 void sk_color_premultiply_array(const sk_color_t* colors, int size, sk_pmcolor_t* pmcolors)
94 {
95     for (int i = 0; i < size; ++i) {
96         pmcolors[i] = SkPreMultiplyColor(colors[i]);
97     }
98 }
99
100 void sk_color_get_bit_shift(int* a, int* r, int* g, int* b)
101 {
102     *a = (int)SK_A32_SHIFT;
103     *r = (int)SK_R32_SHIFT;
104     *g = (int)SK_G32_SHIFT;
105     *b = (int)SK_B32_SHIFT;
106 }
107
108 bool sk_pixmap_encode_image(sk_wstream_t* dst, const sk_pixmap_t* src, sk_encoded_image_format_t encoder, int quality)
109 {
110     return SkEncodeImage(AsWStream(dst), AsPixmap(*src), (SkEncodedImageFormat)encoder, quality);
111 }
112
113 bool sk_pixmap_read_pixels(const sk_pixmap_t* cpixmap, const sk_imageinfo_t* dstInfo, void* dstPixels, size_t dstRowBytes, int srcX, int srcY)
114 {
115     SkImageInfo info;
116     from_c(*dstInfo, &info);
117
118     return AsPixmap(cpixmap)->readPixels(info, dstPixels, dstRowBytes, srcX, srcY);
119 }
120
121 void sk_swizzle_swap_rb(uint32_t* dest, const uint32_t* src, int count)
122 {
123     SkSwapRB(dest, src, count);
124 }