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