reed@google.com | 6997ebb | 2012-07-30 19:50:31 +0000 | [diff] [blame] | 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 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 8 | #include "SkImagePriv.h" |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkPicture.h" |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 11 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 12 | SkBitmap::Config SkImageInfoToBitmapConfig(const SkImageInfo& info) { |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 13 | switch (info.fColorType) { |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 14 | case kAlpha_8_SkColorType: |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 15 | return SkBitmap::kA8_Config; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 16 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 17 | case kRGB_565_SkColorType: |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 18 | return SkBitmap::kRGB_565_Config; |
| 19 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 20 | case kPMColor_SkColorType: |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 21 | return SkBitmap::kARGB_8888_Config; |
skia.committer@gmail.com | 572a865 | 2013-09-21 07:01:53 +0000 | [diff] [blame] | 22 | |
reed@google.com | 23be7a5 | 2013-11-12 16:11:07 +0000 | [diff] [blame^] | 23 | case kIndex8_SkColorType: |
| 24 | return SkBitmap::kIndex8_Config; |
| 25 | |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 26 | default: |
| 27 | // break for unsupported colortypes |
| 28 | break; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 29 | } |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 30 | return SkBitmap::kNo_Config; |
| 31 | } |
| 32 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 33 | int SkImageBytesPerPixel(SkColorType ct) { |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 34 | static const uint8_t gColorTypeBytesPerPixel[] = { |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 35 | 1, // kAlpha_8_SkColorType |
| 36 | 2, // kRGB_565_SkColorType |
| 37 | 4, // kRGBA_8888_SkColorType |
| 38 | 4, // kBGRA_8888_SkColorType |
| 39 | 4, // kPMColor_SkColorType |
reed@google.com | 23be7a5 | 2013-11-12 16:11:07 +0000 | [diff] [blame^] | 40 | 1, // kIndex8_SkColorType |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | SkASSERT((size_t)ct < SK_ARRAY_COUNT(gColorTypeBytesPerPixel)); |
| 44 | return gColorTypeBytesPerPixel[ct]; |
| 45 | } |
| 46 | |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 47 | bool SkBitmapToImageInfo(const SkBitmap& bm, SkImageInfo* info) { |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 48 | switch (bm.config()) { |
| 49 | case SkBitmap::kA8_Config: |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 50 | info->fColorType = kAlpha_8_SkColorType; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 51 | break; |
reed@google.com | 23be7a5 | 2013-11-12 16:11:07 +0000 | [diff] [blame^] | 52 | |
| 53 | case SkBitmap::kIndex8_Config: |
| 54 | info->fColorType = kIndex8_SkColorType; |
| 55 | break; |
| 56 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 57 | case SkBitmap::kRGB_565_Config: |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 58 | info->fColorType = kRGB_565_SkColorType; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 59 | break; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 60 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 61 | case SkBitmap::kARGB_8888_Config: |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 62 | info->fColorType = kPMColor_SkColorType; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 63 | break; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 64 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 65 | default: |
| 66 | return false; |
| 67 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 68 | |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 69 | info->fWidth = bm.width(); |
| 70 | info->fHeight = bm.height(); |
reed@google.com | d28ba80 | 2013-09-20 19:33:52 +0000 | [diff] [blame] | 71 | info->fAlphaType = bm.isOpaque() ? kOpaque_SkAlphaType : |
| 72 | kPremul_SkAlphaType; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 76 | SkImage* SkNewImageFromBitmap(const SkBitmap& bm, bool canSharePixelRef) { |
reed@google.com | 2bd8b81 | 2013-11-01 13:46:54 +0000 | [diff] [blame] | 77 | SkImageInfo info; |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 78 | if (!SkBitmapToImageInfo(bm, &info)) { |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | SkImage* image = NULL; |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 83 | if (canSharePixelRef || bm.isImmutable()) { |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 84 | image = SkNewImageFromPixelRef(info, bm.pixelRef(), bm.rowBytes()); |
| 85 | } else { |
| 86 | bm.lockPixels(); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 87 | if (bm.getPixels()) { |
mike@reedtribe.org | b947625 | 2012-11-15 02:37:45 +0000 | [diff] [blame] | 88 | image = SkImage::NewRasterCopy(info, bm.getPixels(), bm.rowBytes()); |
reed@google.com | 889b09e | 2012-07-27 21:10:42 +0000 | [diff] [blame] | 89 | } |
| 90 | bm.unlockPixels(); |
| 91 | } |
| 92 | return image; |
| 93 | } |
| 94 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 95 | static bool needs_layer(const SkPaint& paint) { |
| 96 | return 0xFF != paint.getAlpha() || |
| 97 | paint.getColorFilter() || |
| 98 | paint.getImageFilter() || |
| 99 | SkXfermode::IsMode(paint.getXfermode(), SkXfermode::kSrcOver_Mode); |
| 100 | } |
| 101 | |
| 102 | void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture, |
| 103 | SkScalar x, SkScalar y, const SkPaint* paint) { |
| 104 | int saveCount = canvas->getSaveCount(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 105 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 106 | if (paint && needs_layer(*paint)) { |
| 107 | SkRect bounds; |
| 108 | bounds.set(x, y, |
| 109 | x + SkIntToScalar(picture->width()), |
| 110 | y + SkIntToScalar(picture->height())); |
| 111 | canvas->saveLayer(&bounds, paint); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 112 | canvas->translate(x, y); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 113 | } else if (x || y) { |
| 114 | canvas->save(); |
reed@google.com | 9ea5a3b | 2012-07-30 21:03:46 +0000 | [diff] [blame] | 115 | canvas->translate(x, y); |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 116 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 117 | |
mike@reedtribe.org | 70e3590 | 2012-07-29 20:38:16 +0000 | [diff] [blame] | 118 | canvas->drawPicture(*picture); |
| 119 | canvas->restoreToCount(saveCount); |
| 120 | } |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 121 | |
| 122 | void SkImagePrivDrawPicture(SkCanvas* canvas, SkPicture* picture, |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 123 | const SkRect* src, const SkRect& dst, const SkPaint* paint) { |
| 124 | int saveCount = canvas->getSaveCount(); |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 125 | |
| 126 | SkMatrix matrix; |
| 127 | SkRect tmpSrc; |
| 128 | |
| 129 | if (NULL != src) { |
| 130 | tmpSrc = *src; |
| 131 | } else { |
| 132 | tmpSrc.set(0, 0, |
| 133 | SkIntToScalar(picture->width()), |
| 134 | SkIntToScalar(picture->height())); |
| 135 | } |
| 136 | |
| 137 | matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit); |
| 138 | if (paint && needs_layer(*paint)) { |
| 139 | canvas->saveLayer(&dst, paint); |
| 140 | } else { |
| 141 | canvas->save(); |
| 142 | } |
| 143 | canvas->concat(matrix); |
| 144 | if (!paint || !needs_layer(*paint)) { |
| 145 | canvas->clipRect(tmpSrc); |
| 146 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 147 | |
commit-bot@chromium.org | dfec28d | 2013-07-23 15:52:16 +0000 | [diff] [blame] | 148 | canvas->drawPicture(*picture); |
| 149 | canvas->restoreToCount(saveCount); |
| 150 | } |