1 /*
2 * Copyright 2012 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
8 #include "SkImagePriv.h"
9 #include "SkCanvas.h"
10 #include "SkPicture.h"
11
12 #ifdef SK_SUPPORT_LEGACY_BITMAP_CONFIG
SkColorTypeToBitmapConfig(SkColorType colorType)13 SkBitmap::Config SkColorTypeToBitmapConfig(SkColorType colorType) {
14 switch (colorType) {
15 case kAlpha_8_SkColorType:
16 return SkBitmap::kA8_Config;
17
18 case kARGB_4444_SkColorType:
19 return SkBitmap::kARGB_4444_Config;
20
21 case kRGB_565_SkColorType:
22 return SkBitmap::kRGB_565_Config;
23
24 case kN32_SkColorType:
25 return SkBitmap::kARGB_8888_Config;
26
27 case kIndex_8_SkColorType:
28 return SkBitmap::kIndex8_Config;
29
30 default:
31 // break for unsupported colortypes
32 break;
33 }
34 return SkBitmap::kNo_Config;
35 }
36
SkBitmapConfigToColorType(SkBitmap::Config config)37 SkColorType SkBitmapConfigToColorType(SkBitmap::Config config) {
38 static const SkColorType gCT[] = {
39 kUnknown_SkColorType, // kNo_Config
40 kAlpha_8_SkColorType, // kA8_Config
41 kIndex_8_SkColorType, // kIndex8_Config
42 kRGB_565_SkColorType, // kRGB_565_Config
43 kARGB_4444_SkColorType, // kARGB_4444_Config
44 kN32_SkColorType, // kARGB_8888_Config
45 };
46 SkASSERT((unsigned)config < SK_ARRAY_COUNT(gCT));
47 return gCT[config];
48 }
49 #endif
50
SkNewImageFromBitmap(const SkBitmap & bm,bool canSharePixelRef)51 SkImage* SkNewImageFromBitmap(const SkBitmap& bm, bool canSharePixelRef) {
52 const SkImageInfo info = bm.info();
53 if (kUnknown_SkColorType == info.colorType()) {
54 return NULL;
55 }
56
57 SkImage* image = NULL;
58 if (canSharePixelRef || bm.isImmutable()) {
59 image = SkNewImageFromPixelRef(info, bm.pixelRef(), bm.rowBytes());
60 } else {
61 bm.lockPixels();
62 if (bm.getPixels()) {
63 image = SkImage::NewRasterCopy(info, bm.getPixels(), bm.rowBytes());
64 }
65 bm.unlockPixels();
66 }
67 return image;
68 }
69