Image.h revision 21efb6827cede06c2ab708de6cdb64d052dddcce
1/*
2 * Copyright (C) 2016 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
17#ifndef AAPT_COMPILE_IMAGE_H
18#define AAPT_COMPILE_IMAGE_H
19
20#include <android-base/macros.h>
21#include <cstdint>
22#include <memory>
23#include <string>
24#include <vector>
25
26namespace aapt {
27
28/**
29 * An in-memory image, loaded from disk, with pixels in RGBA_8888 format.
30 */
31class Image {
32public:
33    explicit Image() = default;
34
35    /**
36     * A `height` sized array of pointers, where each element points to a
37     * `width` sized row of RGBA_8888 pixels.
38     */
39    std::unique_ptr<uint8_t*[]> rows;
40
41    /**
42     * The width of the image in RGBA_8888 pixels. This is int32_t because of 9-patch data
43     * format limitations.
44     */
45    int32_t width = 0;
46
47    /**
48     * The height of the image in RGBA_8888 pixels. This is int32_t because of 9-patch data
49     * format limitations.
50     */
51    int32_t height = 0;
52
53    /**
54     * Buffer to the raw image data stored sequentially.
55     * Use `rows` to access the data on a row-by-row basis.
56     */
57    std::unique_ptr<uint8_t[]> data;
58
59private:
60    DISALLOW_COPY_AND_ASSIGN(Image);
61};
62
63/**
64 * A range of pixel values, starting at 'start' and ending before 'end' exclusive. Or rather [a, b).
65 */
66struct Range {
67    int32_t start = 0;
68    int32_t end = 0;
69
70    explicit Range() = default;
71    inline explicit Range(int32_t s, int32_t e) : start(s), end(e) {
72    }
73};
74
75inline bool operator==(const Range& left, const Range& right) {
76    return left.start == right.start && left.end == right.end;
77}
78
79/**
80 * Inset lengths from all edges of a rectangle. `left` and `top` are measured from the left and top
81 * edges, while `right` and `bottom` are measured from the right and bottom edges, respectively.
82 */
83struct Bounds {
84    int32_t left = 0;
85    int32_t top = 0;
86    int32_t right = 0;
87    int32_t bottom = 0;
88
89    explicit Bounds() = default;
90    inline explicit Bounds(int32_t l, int32_t t, int32_t r, int32_t b) :
91            left(l), top(t), right(r), bottom(b) {
92    }
93
94    bool nonZero() const;
95};
96
97inline bool Bounds::nonZero() const {
98    return left != 0 || top != 0 || right != 0 || bottom != 0;
99}
100
101inline bool operator==(const Bounds& left, const Bounds& right) {
102    return left.left == right.left && left.top == right.top &&
103            left.right == right.right && left.bottom == right.bottom;
104}
105
106/**
107 * Contains 9-patch data from a source image. All measurements exclude the 1px border of the
108 * source 9-patch image.
109 */
110class NinePatch {
111public:
112    static std::unique_ptr<NinePatch> create(uint8_t** rows,
113                                             const int32_t width, const int32_t height,
114                                             std::string* errOut);
115
116    /**
117     * Packs the RGBA_8888 data pointed to by pixel into a uint32_t
118     * with format 0xAARRGGBB (the way 9-patch expects it).
119     */
120    static uint32_t packRGBA(const uint8_t* pixel);
121
122    /**
123     * 9-patch content padding/insets. All positions are relative to the 9-patch
124     * NOT including the 1px thick source border.
125     */
126    Bounds padding;
127
128    /**
129     * Optical layout bounds/insets. This overrides the padding for
130     * layout purposes. All positions are relative to the 9-patch
131     * NOT including the 1px thick source border.
132     * See https://developer.android.com/about/versions/android-4.3.html#OpticalBounds
133     */
134    Bounds layoutBounds;
135
136    /**
137     * Outline of the image, calculated based on opacity.
138     */
139    Bounds outline;
140
141    /**
142     * The computed radius of the outline. If non-zero, the outline is a rounded-rect.
143     */
144    float outlineRadius = 0.0f;
145
146    /**
147     * The largest alpha value within the outline.
148     */
149    uint32_t outlineAlpha = 0x000000ffu;
150
151    /**
152     * Horizontal regions of the image that are stretchable.
153     * All positions are relative to the 9-patch
154     * NOT including the 1px thick source border.
155     */
156    std::vector<Range> horizontalStretchRegions;
157
158    /**
159     * Vertical regions of the image that are stretchable.
160     * All positions are relative to the 9-patch
161     * NOT including the 1px thick source border.
162     */
163    std::vector<Range> verticalStretchRegions;
164
165    /**
166     * The colors within each region, fixed or stretchable.
167     * For w*h regions, the color of region (x,y) is addressable
168     * via index y*w + x.
169     */
170    std::vector<uint32_t> regionColors;
171
172    /**
173     * Returns serialized data containing the original basic 9-patch meta data.
174     * Optical layout bounds and round rect outline data must be serialized
175     * separately using serializeOpticalLayoutBounds() and serializeRoundedRectOutline().
176     */
177    std::unique_ptr<uint8_t[]> serializeBase(size_t* outLen) const;
178
179    /**
180     * Serializes the layout bounds.
181     */
182    std::unique_ptr<uint8_t[]> serializeLayoutBounds(size_t* outLen) const;
183
184    /**
185     * Serializes the rounded-rect outline.
186     */
187    std::unique_ptr<uint8_t[]> serializeRoundedRectOutline(size_t* outLen) const;
188
189private:
190    explicit NinePatch() = default;
191
192    DISALLOW_COPY_AND_ASSIGN(NinePatch);
193};
194
195::std::ostream& operator<<(::std::ostream& out, const Range& range);
196::std::ostream& operator<<(::std::ostream& out, const Bounds& bounds);
197::std::ostream& operator<<(::std::ostream& out, const NinePatch& ninePatch);
198
199} // namespace aapt
200
201#endif /* AAPT_COMPILE_IMAGE_H */
202