1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <SkCodec.h>
19 #include <SkImageInfo.h>
20 #include <SkPngChunkReader.h>
21 #include <SkRect.h>
22 #include <SkSize.h>
23 #include <cutils/compiler.h>
24 
25 #include <optional>
26 
27 class SkAndroidCodec;
28 
29 namespace android {
30 
31 class ANDROID_API ImageDecoder {
32 public:
33     std::unique_ptr<SkAndroidCodec> mCodec;
34     sk_sp<SkPngChunkReader> mPeeker;
35 
36     ImageDecoder(std::unique_ptr<SkAndroidCodec> codec,
37                  sk_sp<SkPngChunkReader> peeker = nullptr);
38 
39     bool setTargetSize(int width, int height);
40     bool setCropRect(const SkIRect*);
41 
42     bool setOutColorType(SkColorType outColorType);
43 
44     bool setUnpremultipliedRequired(bool unpremultipliedRequired);
45 
46     sk_sp<SkColorSpace> getDefaultColorSpace() const;
47     void setOutColorSpace(sk_sp<SkColorSpace> cs);
48 
49     // The size is the final size after scaling and cropping.
50     SkImageInfo getOutputInfo() const;
51 
52     bool opaque() const;
53     bool gray() const;
54 
55     SkCodec::Result decode(void* pixels, size_t rowBytes);
56 
57 private:
58     SkISize mTargetSize;
59     SkISize mDecodeSize;
60     SkColorType mOutColorType;
61     bool mUnpremultipliedRequired;
62     sk_sp<SkColorSpace> mOutColorSpace;
63     int mSampleSize;
64     std::optional<SkIRect> mCropRect;
65 
66     ImageDecoder(const ImageDecoder&) = delete;
67     ImageDecoder& operator=(const ImageDecoder&) = delete;
68 
69     SkAlphaType getOutAlphaType() const;
70     sk_sp<SkColorSpace> getOutputColorSpace() const;
71 };
72 
73 } // namespace android
74