• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 Google LLC
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 "tools/DecodeUtils.h"
9 
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkData.h"
14 #include "include/core/SkImageGenerator.h"
15 #include "include/core/SkImageInfo.h"
16 #include "src/image/SkImageGeneratorPriv.h"
17 
18 #include <memory>
19 #include <utility>
20 
21 namespace ToolUtils {
22 
DecodeDataToBitmap(sk_sp<SkData> data,SkBitmap * dst)23 bool DecodeDataToBitmap(sk_sp<SkData> data, SkBitmap* dst) {
24     std::unique_ptr<SkImageGenerator> gen(SkImageGenerators::MakeFromEncoded(std::move(data)));
25     return gen && dst->tryAllocPixels(gen->getInfo()) &&
26            gen->getPixels(
27                    gen->getInfo().makeColorSpace(nullptr), dst->getPixels(), dst->rowBytes());
28 }
29 
DecodeDataToBitmapWithColorType(sk_sp<SkData> data,SkBitmap * dst,SkColorType dstCT)30 bool DecodeDataToBitmapWithColorType(sk_sp<SkData> data, SkBitmap* dst, SkColorType dstCT) {
31   std::unique_ptr<SkImageGenerator> gen(SkImageGenerators::MakeFromEncoded(std::move(data)));
32   return gen && dst->tryAllocPixels(gen->getInfo().makeColorType(dstCT)) &&
33          gen->getPixels(
34                  gen->getInfo().makeColorSpace(nullptr).makeColorType(dstCT),
35                  dst->getPixels(), dst->rowBytes());
36 }
37 
38 }  // namespace ToolUtils
39