blob: 4cf2ea788b5cc942bbcb63fc28d00b6d52f279bc [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
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 {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 public:
33 explicit Image() = default;
Adam Lesinski21efb682016-09-14 17:35:43 -070034
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 /**
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;
Adam Lesinski21efb682016-09-14 17:35:43 -070040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 /**
42 * The width of the image in RGBA_8888 pixels. This is int32_t because of
43 * 9-patch data
44 * format limitations.
45 */
46 int32_t width = 0;
Adam Lesinski21efb682016-09-14 17:35:43 -070047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 /**
49 * The height of the image in RGBA_8888 pixels. This is int32_t because of
50 * 9-patch data
51 * format limitations.
52 */
53 int32_t height = 0;
Adam Lesinski21efb682016-09-14 17:35:43 -070054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 /**
56 * Buffer to the raw image data stored sequentially.
57 * Use `rows` to access the data on a row-by-row basis.
58 */
59 std::unique_ptr<uint8_t[]> data;
Adam Lesinski21efb682016-09-14 17:35:43 -070060
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 private:
62 DISALLOW_COPY_AND_ASSIGN(Image);
Adam Lesinski21efb682016-09-14 17:35:43 -070063};
64
65/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 * A range of pixel values, starting at 'start' and ending before 'end'
67 * exclusive. Or rather [a, b).
Adam Lesinski21efb682016-09-14 17:35:43 -070068 */
69struct Range {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 int32_t start = 0;
71 int32_t end = 0;
Adam Lesinski21efb682016-09-14 17:35:43 -070072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 explicit Range() = default;
74 inline explicit Range(int32_t s, int32_t e) : start(s), end(e) {}
Adam Lesinski21efb682016-09-14 17:35:43 -070075};
76
77inline bool operator==(const Range& left, const Range& right) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 return left.start == right.start && left.end == right.end;
Adam Lesinski21efb682016-09-14 17:35:43 -070079}
80
81/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 * Inset lengths from all edges of a rectangle. `left` and `top` are measured
83 * from the left and top
84 * edges, while `right` and `bottom` are measured from the right and bottom
85 * edges, respectively.
Adam Lesinski21efb682016-09-14 17:35:43 -070086 */
87struct Bounds {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 int32_t left = 0;
89 int32_t top = 0;
90 int32_t right = 0;
91 int32_t bottom = 0;
Adam Lesinski21efb682016-09-14 17:35:43 -070092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 explicit Bounds() = default;
94 inline explicit Bounds(int32_t l, int32_t t, int32_t r, int32_t b)
95 : left(l), top(t), right(r), bottom(b) {}
Adam Lesinski21efb682016-09-14 17:35:43 -070096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 bool nonZero() const;
Adam Lesinski21efb682016-09-14 17:35:43 -070098};
99
100inline bool Bounds::nonZero() const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101 return left != 0 || top != 0 || right != 0 || bottom != 0;
Adam Lesinski21efb682016-09-14 17:35:43 -0700102}
103
104inline bool operator==(const Bounds& left, const Bounds& right) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 return left.left == right.left && left.top == right.top &&
106 left.right == right.right && left.bottom == right.bottom;
Adam Lesinski21efb682016-09-14 17:35:43 -0700107}
108
109/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 * Contains 9-patch data from a source image. All measurements exclude the 1px
111 * border of the
Adam Lesinski21efb682016-09-14 17:35:43 -0700112 * source 9-patch image.
113 */
114class NinePatch {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 public:
116 static std::unique_ptr<NinePatch> create(uint8_t** rows, const int32_t width,
117 const int32_t height,
118 std::string* errOut);
Adam Lesinski21efb682016-09-14 17:35:43 -0700119
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 /**
121 * Packs the RGBA_8888 data pointed to by pixel into a uint32_t
122 * with format 0xAARRGGBB (the way 9-patch expects it).
123 */
124 static uint32_t packRGBA(const uint8_t* pixel);
Adam Lesinski21efb682016-09-14 17:35:43 -0700125
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 /**
127 * 9-patch content padding/insets. All positions are relative to the 9-patch
128 * NOT including the 1px thick source border.
129 */
130 Bounds padding;
Adam Lesinski21efb682016-09-14 17:35:43 -0700131
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 /**
133 * Optical layout bounds/insets. This overrides the padding for
134 * layout purposes. All positions are relative to the 9-patch
135 * NOT including the 1px thick source border.
136 * See
137 * https://developer.android.com/about/versions/android-4.3.html#OpticalBounds
138 */
139 Bounds layoutBounds;
Adam Lesinski21efb682016-09-14 17:35:43 -0700140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 /**
142 * Outline of the image, calculated based on opacity.
143 */
144 Bounds outline;
Adam Lesinski21efb682016-09-14 17:35:43 -0700145
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 /**
147 * The computed radius of the outline. If non-zero, the outline is a
148 * rounded-rect.
149 */
150 float outlineRadius = 0.0f;
Adam Lesinski21efb682016-09-14 17:35:43 -0700151
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 /**
153 * The largest alpha value within the outline.
154 */
155 uint32_t outlineAlpha = 0x000000ffu;
Adam Lesinski21efb682016-09-14 17:35:43 -0700156
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 /**
158 * Horizontal regions of the image that are stretchable.
159 * All positions are relative to the 9-patch
160 * NOT including the 1px thick source border.
161 */
162 std::vector<Range> horizontalStretchRegions;
Adam Lesinski21efb682016-09-14 17:35:43 -0700163
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 /**
165 * Vertical regions of the image that are stretchable.
166 * All positions are relative to the 9-patch
167 * NOT including the 1px thick source border.
168 */
169 std::vector<Range> verticalStretchRegions;
Adam Lesinski21efb682016-09-14 17:35:43 -0700170
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 /**
172 * The colors within each region, fixed or stretchable.
173 * For w*h regions, the color of region (x,y) is addressable
174 * via index y*w + x.
175 */
176 std::vector<uint32_t> regionColors;
Adam Lesinski21efb682016-09-14 17:35:43 -0700177
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 /**
179 * Returns serialized data containing the original basic 9-patch meta data.
180 * Optical layout bounds and round rect outline data must be serialized
181 * separately using serializeOpticalLayoutBounds() and
182 * serializeRoundedRectOutline().
183 */
184 std::unique_ptr<uint8_t[]> serializeBase(size_t* outLen) const;
Adam Lesinski21efb682016-09-14 17:35:43 -0700185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186 /**
187 * Serializes the layout bounds.
188 */
189 std::unique_ptr<uint8_t[]> serializeLayoutBounds(size_t* outLen) const;
Adam Lesinski21efb682016-09-14 17:35:43 -0700190
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 /**
192 * Serializes the rounded-rect outline.
193 */
194 std::unique_ptr<uint8_t[]> serializeRoundedRectOutline(size_t* outLen) const;
Adam Lesinski21efb682016-09-14 17:35:43 -0700195
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700196 private:
197 explicit NinePatch() = default;
Adam Lesinski21efb682016-09-14 17:35:43 -0700198
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700199 DISALLOW_COPY_AND_ASSIGN(NinePatch);
Adam Lesinski21efb682016-09-14 17:35:43 -0700200};
201
202::std::ostream& operator<<(::std::ostream& out, const Range& range);
203::std::ostream& operator<<(::std::ostream& out, const Bounds& bounds);
204::std::ostream& operator<<(::std::ostream& out, const NinePatch& ninePatch);
205
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206} // namespace aapt
Adam Lesinski21efb682016-09-14 17:35:43 -0700207
208#endif /* AAPT_COMPILE_IMAGE_H */