• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
10 #include "SkColorPriv.h"
11 #include "SkHalf.h"
12 #include "SkImageEncoder.h"
13 #include "SkOSFile.h"
14 #include "SkOSPath.h"
15 #include "SkPM4fPriv.h"
16 #include "SkPicture.h"
17 #include "SkStream.h"
18 #include "SkString.h"
19 #include "SkRasterPipeline.h"
20 
21 #include "sk_tool_utils.h"
22 
23 namespace sk_tools {
force_all_opaque(const SkBitmap & bitmap)24     void force_all_opaque(const SkBitmap& bitmap) {
25         SkASSERT(kN32_SkColorType == bitmap.colorType());
26         if (kN32_SkColorType == bitmap.colorType()) {
27             return;
28         }
29 
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     }
36 
replace_char(SkString * str,const char oldChar,const char newChar)37     void replace_char(SkString* str, const char oldChar, const char newChar) {
38         if (nullptr == str) {
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 
is_percentage(const char * const string)48     bool is_percentage(const char* const string) {
49         SkString skString(string);
50         return skString.endsWith("%");
51     }
52 
setup_bitmap(SkBitmap * bitmap,int width,int height)53     void setup_bitmap(SkBitmap* bitmap, int width, int height) {
54         bitmap->allocN32Pixels(width, height);
55         bitmap->eraseColor(SK_ColorTRANSPARENT);
56     }
57 
write_bitmap_to_disk(const SkBitmap & bm,const SkString & dirPath,const char * subdirOrNull,const SkString & baseName)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) {
62             partialPath = SkOSPath::Join(dirPath.c_str(), subdirOrNull);
63             sk_mkdir(partialPath.c_str());
64         } else {
65             partialPath.set(dirPath);
66         }
67         SkString fullPath = SkOSPath::Join(partialPath.c_str(), baseName.c_str());
68         if (sk_tool_utils::EncodeImageToFile(fullPath.c_str(), bm, SkEncodedImageFormat::kPNG, 100)) {
69             return true;
70         } else {
71             SkDebugf("Failed to write the bitmap to %s.\n", fullPath.c_str());
72             return false;
73         }
74     }
75 
encode_bitmap_for_png(SkBitmap bitmap)76     sk_sp<SkData> encode_bitmap_for_png(SkBitmap bitmap) {
77         const int w = bitmap.width(),
78                   h = bitmap.height();
79 
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 
86         const void* src = bitmap.getPixels();
87         uint32_t*   dst = rgba.get();
88 
89         SkRasterPipeline_<256> p;
90         switch (bitmap.colorType()) {
91             case  kRGBA_F16_SkColorType: p.append(SkRasterPipeline::load_f16,  &src); break;
92             case kBGRA_8888_SkColorType: p.append(SkRasterPipeline::load_bgra, &src); break;
93             case kRGBA_8888_SkColorType: p.append(SkRasterPipeline::load_8888, &src); break;
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()) {
98             p.append_from_srgb(kUnpremul_SkAlphaType);
99         }
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);
108 
109         auto run = p.compile();
110         for (int y = 0; y < h; y++) {
111             run(0,y, w);
112             src = SkTAddOffset<const void>(src, bitmap.rowBytes());
113             dst += w;
114         }
115 
116         return SkData::MakeFromMalloc(rgba.release(), w*h*sizeof(uint32_t));
117     }
118 
119 } // namespace sk_tools
120