Fixing paths messed up in previous sel_ldr.py change.
[chromium-blink-merge.git] / skia / public / type_converters.cc
blob143710b6cc94001c8f4fa41c07acc1fc3d1b6bea
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "skia/public/type_converters.h"
7 #include <string.h>
9 #include "base/logging.h"
10 #include "third_party/skia/include/core/SkBitmap.h"
12 namespace mojo {
14 namespace {
16 SkColorType MojoColorTypeToSk(skia::ColorType type) {
17 switch (type) {
18 case skia::COLOR_TYPE_UNKNOWN:
19 return kUnknown_SkColorType;
20 case skia::COLOR_TYPE_ALPHA_8:
21 return kAlpha_8_SkColorType;
22 case skia::COLOR_TYPE_RGB_565:
23 return kRGB_565_SkColorType;
24 case skia::COLOR_TYPE_ARGB_4444:
25 return kARGB_4444_SkColorType;
26 case skia::COLOR_TYPE_RGBA_8888:
27 return kRGBA_8888_SkColorType;
28 case skia::COLOR_TYPE_BGRA_8888:
29 return kBGRA_8888_SkColorType;
30 case skia::COLOR_TYPE_INDEX_8:
31 return kIndex_8_SkColorType;
32 case skia::COLOR_TYPE_GRAY_8:
33 return kGray_8_SkColorType;
34 default:
35 NOTREACHED();
37 return kUnknown_SkColorType;
40 SkAlphaType MojoAlphaTypeToSk(skia::AlphaType type) {
41 switch (type) {
42 case skia::ALPHA_TYPE_UNKNOWN:
43 return kUnknown_SkAlphaType;
44 case skia::ALPHA_TYPE_OPAQUE:
45 return kOpaque_SkAlphaType;
46 case skia::ALPHA_TYPE_PREMUL:
47 return kPremul_SkAlphaType;
48 case skia::ALPHA_TYPE_UNPREMUL:
49 return kUnpremul_SkAlphaType;
50 default:
51 NOTREACHED();
53 return kUnknown_SkAlphaType;
56 skia::ColorType SkColorTypeToMojo(SkColorType type) {
57 switch (type) {
58 case kUnknown_SkColorType:
59 return skia::COLOR_TYPE_UNKNOWN;
60 case kAlpha_8_SkColorType:
61 return skia::COLOR_TYPE_ALPHA_8;
62 case kRGB_565_SkColorType:
63 return skia::COLOR_TYPE_RGB_565;
64 case kARGB_4444_SkColorType:
65 return skia::COLOR_TYPE_ARGB_4444;
66 case kRGBA_8888_SkColorType:
67 return skia::COLOR_TYPE_RGBA_8888;
68 case kBGRA_8888_SkColorType:
69 return skia::COLOR_TYPE_BGRA_8888;
70 case kIndex_8_SkColorType:
71 return skia::COLOR_TYPE_INDEX_8;
72 case kGray_8_SkColorType:
73 return skia::COLOR_TYPE_GRAY_8;
74 default:
75 NOTREACHED();
77 return skia::COLOR_TYPE_UNKNOWN;
80 skia::AlphaType SkAlphaTypeToMojo(SkAlphaType type) {
81 switch (type) {
82 case kUnknown_SkAlphaType:
83 return skia::ALPHA_TYPE_UNKNOWN;
84 case kOpaque_SkAlphaType:
85 return skia::ALPHA_TYPE_OPAQUE;
86 case kPremul_SkAlphaType:
87 return skia::ALPHA_TYPE_PREMUL;
88 case kUnpremul_SkAlphaType:
89 return skia::ALPHA_TYPE_UNPREMUL;
90 default:
91 NOTREACHED();
93 return skia::ALPHA_TYPE_UNKNOWN;
96 } // namespace
98 SkBitmap TypeConverter<SkBitmap, skia::BitmapPtr>::Convert(
99 const skia::BitmapPtr& image) {
100 SkBitmap bitmap;
101 if (image.is_null())
102 return bitmap;
103 if (!bitmap.tryAllocPixels(SkImageInfo::Make(
104 image->width, image->height, MojoColorTypeToSk(image->color_type),
105 MojoAlphaTypeToSk(image->alpha_type)))) {
106 return SkBitmap();
108 if (bitmap.getSize() != image->pixel_data.size() || !bitmap.getPixels()) {
109 return SkBitmap();
112 memcpy(bitmap.getPixels(), &image->pixel_data[0], bitmap.getSize());
113 return bitmap;
116 skia::BitmapPtr TypeConverter<skia::BitmapPtr, SkBitmap>::Convert(
117 const SkBitmap& bitmap) {
118 if (bitmap.isNull())
119 return nullptr;
121 // NOTE: This code doesn't correctly serialize Index8 bitmaps.
122 const SkImageInfo& info = bitmap.info();
123 DCHECK_NE(info.colorType(), kIndex_8_SkColorType);
124 if (info.colorType() == kIndex_8_SkColorType)
125 return nullptr;
126 skia::BitmapPtr result = skia::Bitmap::New();
127 result->color_type = SkColorTypeToMojo(info.colorType());
128 result->alpha_type = SkAlphaTypeToMojo(info.alphaType());
129 result->width = info.width();
130 result->height = info.height();
131 size_t size = bitmap.getSize();
132 size_t row_bytes = bitmap.rowBytes();
133 result->pixel_data = mojo::Array<uint8_t>::New(size);
134 if (!bitmap.readPixels(info, &result->pixel_data[0], row_bytes, 0, 0))
135 return nullptr;
136 return result.Pass();
139 } // namespace mojo