Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 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 | |
| 26 | namespace aapt { |
| 27 | |
| 28 | /** |
| 29 | * An in-memory image, loaded from disk, with pixels in RGBA_8888 format. |
| 30 | */ |
| 31 | class Image { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 32 | public: |
| 33 | explicit Image() = default; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 34 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 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; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 40 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | /** |
| 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 47 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | /** |
| 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 54 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | /** |
| 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 60 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | private: |
| 62 | DISALLOW_COPY_AND_ASSIGN(Image); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | * A range of pixel values, starting at 'start' and ending before 'end' |
| 67 | * exclusive. Or rather [a, b). |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 68 | */ |
| 69 | struct Range { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 70 | int32_t start = 0; |
| 71 | int32_t end = 0; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 72 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | explicit Range() = default; |
| 74 | inline explicit Range(int32_t s, int32_t e) : start(s), end(e) {} |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | inline bool operator==(const Range& left, const Range& right) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | return left.start == right.start && left.end == right.end; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | * 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 86 | */ |
| 87 | struct Bounds { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | int32_t left = 0; |
| 89 | int32_t top = 0; |
| 90 | int32_t right = 0; |
| 91 | int32_t bottom = 0; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 92 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 93 | 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 96 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | bool nonZero() const; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | inline bool Bounds::nonZero() const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | return left != 0 || top != 0 || right != 0 || bottom != 0; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | inline bool operator==(const Bounds& left, const Bounds& right) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | return left.left == right.left && left.top == right.top && |
| 106 | left.right == right.right && left.bottom == right.bottom; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | * Contains 9-patch data from a source image. All measurements exclude the 1px |
| 111 | * border of the |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 112 | * source 9-patch image. |
| 113 | */ |
| 114 | class NinePatch { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 119 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 120 | /** |
| 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 125 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 126 | /** |
| 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 131 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 132 | /** |
| 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 140 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Outline of the image, calculated based on opacity. |
| 143 | */ |
| 144 | Bounds outline; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 145 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 146 | /** |
| 147 | * The computed radius of the outline. If non-zero, the outline is a |
| 148 | * rounded-rect. |
| 149 | */ |
| 150 | float outlineRadius = 0.0f; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 151 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 152 | /** |
| 153 | * The largest alpha value within the outline. |
| 154 | */ |
| 155 | uint32_t outlineAlpha = 0x000000ffu; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 156 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | /** |
| 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 163 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 164 | /** |
| 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 170 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | /** |
| 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 177 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | /** |
| 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 Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 185 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Serializes the layout bounds. |
| 188 | */ |
| 189 | std::unique_ptr<uint8_t[]> serializeLayoutBounds(size_t* outLen) const; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 190 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 191 | /** |
| 192 | * Serializes the rounded-rect outline. |
| 193 | */ |
| 194 | std::unique_ptr<uint8_t[]> serializeRoundedRectOutline(size_t* outLen) const; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 195 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 196 | private: |
| 197 | explicit NinePatch() = default; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 198 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | DISALLOW_COPY_AND_ASSIGN(NinePatch); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 200 | }; |
| 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 206 | } // namespace aapt |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 207 | |
| 208 | #endif /* AAPT_COMPILE_IMAGE_H */ |