twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +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 | |
| 8 | #include "picture_utils.h" |
| 9 | #include "SkBitmap.h" |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 11 | #include "SkHalf.h" |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
| 13 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 14 | #include "SkOSPath.h" |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 15 | #include "SkPM4fPriv.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 16 | #include "SkPicture.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 17 | #include "SkStream.h" |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 18 | #include "SkString.h" |
Mike Klein | fc33416 | 2017-06-12 08:59:16 -0400 | [diff] [blame] | 19 | #include "SkRasterPipeline.h" |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 20 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 21 | #include "sk_tool_utils.h" |
| 22 | |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 23 | namespace sk_tools { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 24 | void force_all_opaque(const SkBitmap& bitmap) { |
reed | 0689d7b | 2014-06-14 05:30:20 -0700 | [diff] [blame] | 25 | SkASSERT(kN32_SkColorType == bitmap.colorType()); |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 26 | if (kN32_SkColorType == bitmap.colorType()) { |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 27 | return; |
| 28 | } |
| 29 | |
keyar@chromium.org | 9299ede | 2012-08-21 19:05:08 +0000 | [diff] [blame] | 30 | for (int y = 0; y < bitmap.height(); y++) { |
| 31 | for (int x = 0; x < bitmap.width(); x++) { |
| 32 | *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); |
| 33 | } |
| 34 | } |
| 35 | } |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 36 | |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 37 | void replace_char(SkString* str, const char oldChar, const char newChar) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | if (nullptr == str) { |
commit-bot@chromium.org | 24c568c | 2014-04-10 15:39:02 +0000 | [diff] [blame] | 39 | return; |
| 40 | } |
| 41 | for (size_t i = 0; i < str->size(); ++i) { |
| 42 | if (oldChar == str->operator[](i)) { |
| 43 | str->operator[](i) = newChar; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
scroggo@google.com | 58b4ead | 2012-08-31 16:15:22 +0000 | [diff] [blame] | 48 | bool is_percentage(const char* const string) { |
keyar@chromium.org | 163b567 | 2012-08-01 17:53:29 +0000 | [diff] [blame] | 49 | SkString skString(string); |
| 50 | return skString.endsWith("%"); |
| 51 | } |
| 52 | |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 53 | void setup_bitmap(SkBitmap* bitmap, int width, int height) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 54 | bitmap->allocN32Pixels(width, height); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 55 | bitmap->eraseColor(SK_ColorTRANSPARENT); |
twiz@google.com | 6cf5303 | 2012-06-22 18:55:55 +0000 | [diff] [blame] | 56 | } |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 57 | |
| 58 | bool write_bitmap_to_disk(const SkBitmap& bm, const SkString& dirPath, |
| 59 | const char *subdirOrNull, const SkString& baseName) { |
| 60 | SkString partialPath; |
| 61 | if (subdirOrNull) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 62 | partialPath = SkOSPath::Join(dirPath.c_str(), subdirOrNull); |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 63 | sk_mkdir(partialPath.c_str()); |
| 64 | } else { |
| 65 | partialPath.set(dirPath); |
| 66 | } |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 67 | SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str()); |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 68 | if (sk_tool_utils::EncodeImageToFile(fullPath.c_str(), bm, SkEncodedImageFormat::kPNG, 100)) { |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 69 | return true; |
| 70 | } else { |
| 71 | SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str()); |
| 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 76 | sk_sp<SkData> encode_bitmap_for_png(SkBitmap bitmap) { |
| 77 | const int w = bitmap.width(), |
Mike Klein | fc33416 | 2017-06-12 08:59:16 -0400 | [diff] [blame] | 78 | h = bitmap.height(); |
| 79 | |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 80 | // PNG wants unpremultiplied 8-bit RGBA pixels (16-bit could work fine too). |
| 81 | // We leave the gamma of these bytes unspecified, to continue the status quo, |
| 82 | // which we think generally is to interpret them as sRGB. |
| 83 | |
| 84 | SkAutoTMalloc<uint32_t> rgba(w*h); |
| 85 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 86 | SkJumper_MemoryCtx src = { bitmap.getPixels(), bitmap.rowBytesAsPixels() }, |
| 87 | dst = { rgba.get(), w }; |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 88 | |
Mike Klein | fc33416 | 2017-06-12 08:59:16 -0400 | [diff] [blame] | 89 | SkRasterPipeline_<256> p; |
| 90 | switch (bitmap.colorType()) { |
| 91 | case kRGBA_F16_SkColorType: p.append(SkRasterPipeline::load_f16, &src); break; |
Mike Klein | c2d2076 | 2017-06-27 19:53:21 -0400 | [diff] [blame] | 92 | case kBGRA_8888_SkColorType: p.append(SkRasterPipeline::load_bgra, &src); break; |
| 93 | case kRGBA_8888_SkColorType: p.append(SkRasterPipeline::load_8888, &src); break; |
Mike Klein | fc33416 | 2017-06-12 08:59:16 -0400 | [diff] [blame] | 94 | case kRGB_565_SkColorType: p.append(SkRasterPipeline::load_565, &src); break; |
| 95 | default: SkASSERT(false); // DM doesn't support any other formats, does it? |
| 96 | } |
| 97 | if (bitmap.info().gammaCloseToSRGB()) { |
Mike Klein | f1f1162 | 2017-12-18 14:07:31 -0500 | [diff] [blame] | 98 | p.append(SkRasterPipeline::from_srgb); |
Mike Klein | fc33416 | 2017-06-12 08:59:16 -0400 | [diff] [blame] | 99 | } |
Mike Klein | fc33416 | 2017-06-12 08:59:16 -0400 | [diff] [blame] | 100 | p.append(SkRasterPipeline::unpremul); |
| 101 | p.append(SkRasterPipeline::clamp_0); |
| 102 | p.append(SkRasterPipeline::clamp_1); |
| 103 | if (bitmap.info().colorSpace()) { |
| 104 | // We leave legacy modes as-is. They're already sRGB encoded (kind of). |
| 105 | p.append(SkRasterPipeline::to_srgb); |
| 106 | } |
| 107 | p.append(SkRasterPipeline::store_8888, &dst); |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 108 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 109 | p.run(0,0, w,h); |
brianosman | 3c579dc | 2016-04-19 09:18:11 -0700 | [diff] [blame] | 110 | |
| 111 | return SkData::MakeFromMalloc(rgba.release(), w*h*sizeof(uint32_t)); |
| 112 | } |
| 113 | |
commit-bot@chromium.org | 3f04517 | 2014-05-15 15:10:48 +0000 | [diff] [blame] | 114 | } // namespace sk_tools |