blob: db0b945e1f18eec2439394b3cb7b030f96bc40ca [file] [log] [blame]
Adam Lesinski21efb682016-09-14 17:35:43 -07001/*
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
Adam Lesinski21efb682016-09-14 17:35:43 -070020#include <cstdint>
21#include <memory>
22#include <string>
23#include <vector>
24
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025#include "android-base/macros.h"
26
Adam Lesinski21efb682016-09-14 17:35:43 -070027namespace aapt {
28
29/**
30 * An in-memory image, loaded from disk, with pixels in RGBA_8888 format.
31 */
32class Image {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 public:
34 explicit Image() = default;
Adam Lesinski21efb682016-09-14 17:35:43 -070035
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 /**
37 * A `height` sized array of pointers, where each element points to a
38 * `width` sized row of RGBA_8888 pixels.
39 */
40 std::unique_ptr<uint8_t* []> rows;
Adam Lesinski21efb682016-09-14 17:35:43 -070041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 /**
43 * The width of the image in RGBA_8888 pixels. This is int32_t because of
44 * 9-patch data
45 * format limitations.
46 */
47 int32_t width = 0;
Adam Lesinski21efb682016-09-14 17:35:43 -070048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 /**
50 * The height of the image in RGBA_8888 pixels. This is int32_t because of
51 * 9-patch data
52 * format limitations.
53 */
54 int32_t height = 0;
Adam Lesinski21efb682016-09-14 17:35:43 -070055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 /**
57 * Buffer to the raw image data stored sequentially.
58 * Use `rows` to access the data on a row-by-row basis.
59 */
60 std::unique_ptr<uint8_t[]> data;
Adam Lesinski21efb682016-09-14 17:35:43 -070061
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 private:
63 DISALLOW_COPY_AND_ASSIGN(Image);
Adam Lesinski21efb682016-09-14 17:35:43 -070064};
65
66/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 * A range of pixel values, starting at 'start' and ending before 'end'
68 * exclusive. Or rather [a, b).
Adam Lesinski21efb682016-09-14 17:35:43 -070069 */
70struct Range {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 int32_t start = 0;
72 int32_t end = 0;
Adam Lesinski21efb682016-09-14 17:35:43 -070073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 explicit Range() = default;
75 inline explicit Range(int32_t s, int32_t e) : start(s), end(e) {}
Adam Lesinski21efb682016-09-14 17:35:43 -070076};
77
78inline bool operator==(const Range& left, const Range& right) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 return left.start == right.start && left.end == right.end;
Adam Lesinski21efb682016-09-14 17:35:43 -070080}
81
82/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 * Inset lengths from all edges of a rectangle. `left` and `top` are measured
84 * from the left and top
85 * edges, while `right` and `bottom` are measured from the right and bottom
86 * edges, respectively.
Adam Lesinski21efb682016-09-14 17:35:43 -070087 */
88struct Bounds {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 int32_t left = 0;
90 int32_t top = 0;
91 int32_t right = 0;
92 int32_t bottom = 0;
Adam Lesinski21efb682016-09-14 17:35:43 -070093
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 explicit Bounds() = default;
95 inline explicit Bounds(int32_t l, int32_t t, int32_t r, int32_t b)
96 : left(l), top(t), right(r), bottom(b) {}
Adam Lesinski21efb682016-09-14 17:35:43 -070097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 bool nonZero() const;
Adam Lesinski21efb682016-09-14 17:35:43 -070099};
100
101inline bool Bounds::nonZero() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 return left != 0 || top != 0 || right != 0 || bottom != 0;
Adam Lesinski21efb682016-09-14 17:35:43 -0700103}
104
105inline bool operator==(const Bounds& left, const Bounds& right) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 return left.left == right.left && left.top == right.top &&
107 left.right == right.right && left.bottom == right.bottom;
Adam Lesinski21efb682016-09-14 17:35:43 -0700108}
109
110/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 * Contains 9-patch data from a source image. All measurements exclude the 1px
112 * border of the
Adam Lesinski21efb682016-09-14 17:35:43 -0700113 * source 9-patch image.
114 */
115class NinePatch {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700117 static std::unique_ptr<NinePatch> Create(uint8_t** rows, const int32_t width,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 const int32_t height,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 std::string* err_out);
Adam Lesinski21efb682016-09-14 17:35:43 -0700120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 /**
122 * Packs the RGBA_8888 data pointed to by pixel into a uint32_t
123 * with format 0xAARRGGBB (the way 9-patch expects it).
124 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 static uint32_t PackRGBA(const uint8_t* pixel);
Adam Lesinski21efb682016-09-14 17:35:43 -0700126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127 /**
128 * 9-patch content padding/insets. All positions are relative to the 9-patch
129 * NOT including the 1px thick source border.
130 */
131 Bounds padding;
Adam Lesinski21efb682016-09-14 17:35:43 -0700132
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 /**
134 * Optical layout bounds/insets. This overrides the padding for
135 * layout purposes. All positions are relative to the 9-patch
136 * NOT including the 1px thick source border.
137 * See
138 * https://developer.android.com/about/versions/android-4.3.html#OpticalBounds
139 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 Bounds layout_bounds;
Adam Lesinski21efb682016-09-14 17:35:43 -0700141
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 /**
143 * Outline of the image, calculated based on opacity.
144 */
145 Bounds outline;
Adam Lesinski21efb682016-09-14 17:35:43 -0700146
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 /**
148 * The computed radius of the outline. If non-zero, the outline is a
149 * rounded-rect.
150 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 float outline_radius = 0.0f;
Adam Lesinski21efb682016-09-14 17:35:43 -0700152
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700153 /**
154 * The largest alpha value within the outline.
155 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 uint32_t outline_alpha = 0x000000ffu;
Adam Lesinski21efb682016-09-14 17:35:43 -0700157
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 /**
159 * Horizontal 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 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 std::vector<Range> horizontal_stretch_regions;
Adam Lesinski21efb682016-09-14 17:35:43 -0700164
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 /**
166 * Vertical regions of the image that are stretchable.
167 * All positions are relative to the 9-patch
168 * NOT including the 1px thick source border.
169 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 std::vector<Range> vertical_stretch_regions;
Adam Lesinski21efb682016-09-14 17:35:43 -0700171
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 /**
173 * The colors within each region, fixed or stretchable.
174 * For w*h regions, the color of region (x,y) is addressable
175 * via index y*w + x.
176 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 std::vector<uint32_t> region_colors;
Adam Lesinski21efb682016-09-14 17:35:43 -0700178
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700179 /**
180 * Returns serialized data containing the original basic 9-patch meta data.
181 * Optical layout bounds and round rect outline data must be serialized
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 * separately using SerializeOpticalLayoutBounds() and
183 * SerializeRoundedRectOutline().
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 std::unique_ptr<uint8_t[]> SerializeBase(size_t* out_len) const;
Adam Lesinski21efb682016-09-14 17:35:43 -0700186
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187 /**
188 * Serializes the layout bounds.
189 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 std::unique_ptr<uint8_t[]> SerializeLayoutBounds(size_t* out_len) const;
Adam Lesinski21efb682016-09-14 17:35:43 -0700191
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700192 /**
193 * Serializes the rounded-rect outline.
194 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700195 std::unique_ptr<uint8_t[]> SerializeRoundedRectOutline(size_t* out_len) const;
Adam Lesinski21efb682016-09-14 17:35:43 -0700196
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 private:
198 explicit NinePatch() = default;
Adam Lesinski21efb682016-09-14 17:35:43 -0700199
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 DISALLOW_COPY_AND_ASSIGN(NinePatch);
Adam Lesinski21efb682016-09-14 17:35:43 -0700201};
202
203::std::ostream& operator<<(::std::ostream& out, const Range& range);
204::std::ostream& operator<<(::std::ostream& out, const Bounds& bounds);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700205::std::ostream& operator<<(::std::ostream& out, const NinePatch& nine_patch);
Adam Lesinski21efb682016-09-14 17:35:43 -0700206
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700207} // namespace aapt
Adam Lesinski21efb682016-09-14 17:35:43 -0700208
209#endif /* AAPT_COMPILE_IMAGE_H */