reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame^] | 1 | #include "SkImagePriv.h" |
| 2 | |
| 3 | SkBitmap::Config SkImageInfoToBitmapConfig(const SkImage::Info& info, |
| 4 | bool* isOpaque) { |
| 5 | switch (info.fColorType) { |
| 6 | case SkImage::kAlpha_8_ColorType: |
| 7 | switch (info.fAlphaType) { |
| 8 | case SkImage::kIgnore_AlphaType: |
| 9 | // makes no sense |
| 10 | return SkBitmap::kNo_Config; |
| 11 | |
| 12 | case SkImage::kOpaque_AlphaType: |
| 13 | *isOpaque = true; |
| 14 | return SkBitmap::kA8_Config; |
| 15 | |
| 16 | case SkImage::kPremul_AlphaType: |
| 17 | case SkImage::kUnpremul_AlphaType: |
| 18 | *isOpaque = false; |
| 19 | return SkBitmap::kA8_Config; |
| 20 | } |
| 21 | break; |
| 22 | |
| 23 | case SkImage::kRGB_565_ColorType: |
| 24 | // we ignore fAlpahType, though some would not make sense |
| 25 | *isOpaque = true; |
| 26 | return SkBitmap::kRGB_565_Config; |
| 27 | |
| 28 | case SkImage::kRGBA_8888_ColorType: |
| 29 | case SkImage::kBGRA_8888_ColorType: |
| 30 | // not supported yet |
| 31 | return SkBitmap::kNo_Config; |
| 32 | |
| 33 | case SkImage::kPMColor_ColorType: |
| 34 | switch (info.fAlphaType) { |
| 35 | case SkImage::kIgnore_AlphaType: |
| 36 | case SkImage::kUnpremul_AlphaType: |
| 37 | // not supported yet |
| 38 | return SkBitmap::kNo_Config; |
| 39 | case SkImage::kOpaque_AlphaType: |
| 40 | *isOpaque = true; |
| 41 | return SkBitmap::kARGB_8888_Config; |
| 42 | case SkImage::kPremul_AlphaType: |
| 43 | *isOpaque = false; |
| 44 | return SkBitmap::kARGB_8888_Config; |
| 45 | } |
| 46 | break; |
| 47 | } |
| 48 | SkASSERT(!"how did we get here"); |
| 49 | return SkBitmap::kNo_Config; |
| 50 | } |
| 51 | |
| 52 | int SkImageBytesPerPixel(SkImage::ColorType ct) { |
| 53 | static const uint8_t gColorTypeBytesPerPixel[] = { |
| 54 | 1, // kAlpha_8_ColorType |
| 55 | 2, // kRGB_565_ColorType |
| 56 | 4, // kRGBA_8888_ColorType |
| 57 | 4, // kBGRA_8888_ColorType |
| 58 | 4, // kPMColor_ColorType |
| 59 | }; |
| 60 | |
| 61 | SkASSERT((size_t)ct < SK_ARRAY_COUNT(gColorTypeBytesPerPixel)); |
| 62 | return gColorTypeBytesPerPixel[ct]; |
| 63 | } |
| 64 | |
| 65 | bool SkBitmapToImageInfo(const SkBitmap& bm, SkImage::Info* info) { |
| 66 | switch (bm.config()) { |
| 67 | case SkBitmap::kA8_Config: |
| 68 | info->fColorType = SkImage::kAlpha_8_ColorType; |
| 69 | break; |
| 70 | |
| 71 | case SkBitmap::kRGB_565_Config: |
| 72 | info->fColorType = SkImage::kRGB_565_ColorType; |
| 73 | break; |
| 74 | |
| 75 | case SkBitmap::kARGB_8888_Config: |
| 76 | info->fColorType = SkImage::kPMColor_ColorType; |
| 77 | break; |
| 78 | |
| 79 | default: |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | info->fWidth = bm.width(); |
| 84 | info->fHeight = bm.height(); |
| 85 | info->fAlphaType = bm.isOpaque() ? SkImage::kOpaque_AlphaType : |
| 86 | SkImage::kPremul_AlphaType; |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | SkImage* SkNewImageFromBitmap(const SkBitmap& bm) { |
| 91 | SkImage::Info info; |
| 92 | if (!SkBitmapToImageInfo(bm, &info)) { |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | SkImage* image = NULL; |
| 97 | if (bm.isImmutable()) { |
| 98 | image = SkNewImageFromPixelRef(info, bm.pixelRef(), bm.rowBytes()); |
| 99 | } else { |
| 100 | bm.lockPixels(); |
| 101 | if (NULL == bm.getPixels()) { |
| 102 | image = SkImage::NewRasterCopy(info, NULL, bm.getPixels(), |
| 103 | bm.rowBytes()); |
| 104 | } |
| 105 | bm.unlockPixels(); |
| 106 | } |
| 107 | return image; |
| 108 | } |
| 109 | |