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 | #include "compile/Image.h" |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 18 | |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 19 | #include <sstream> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 24 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | #include "util/Util.h" |
| 27 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 28 | using android::StringPiece; |
| 29 | |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 30 | namespace aapt { |
| 31 | |
| 32 | // Colors in the format 0xAARRGGBB (the way 9-patch expects it). |
| 33 | constexpr static const uint32_t kColorOpaqueWhite = 0xffffffffu; |
| 34 | constexpr static const uint32_t kColorOpaqueBlack = 0xff000000u; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 35 | constexpr static const uint32_t kColorOpaqueRed = 0xffff0000u; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 36 | |
| 37 | constexpr static const uint32_t kPrimaryColor = kColorOpaqueBlack; |
| 38 | constexpr static const uint32_t kSecondaryColor = kColorOpaqueRed; |
| 39 | |
| 40 | /** |
| 41 | * Returns the alpha value encoded in the 0xAARRGBB encoded pixel. |
| 42 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 43 | static uint32_t get_alpha(uint32_t color); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Determines whether a color on an ImageLine is valid. |
| 47 | * A 9patch image may use a transparent color as neutral, |
| 48 | * or a fully opaque white color as neutral, based on the |
| 49 | * pixel color at (0,0) of the image. One or the other is fine, |
| 50 | * but we need to ensure consistency throughout the image. |
| 51 | */ |
| 52 | class ColorValidator { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | public: |
| 54 | virtual ~ColorValidator() = default; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 55 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | /** |
| 57 | * Returns true if the color specified is a neutral color |
| 58 | * (no padding, stretching, or optical bounds). |
| 59 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | virtual bool IsNeutralColor(uint32_t color) const = 0; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Returns true if the color is either a neutral color |
| 64 | * or one denoting padding, stretching, or optical bounds. |
| 65 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | bool IsValidColor(uint32_t color) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | switch (color) { |
| 68 | case kPrimaryColor: |
| 69 | case kSecondaryColor: |
| 70 | return true; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 71 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | return IsNeutralColor(color); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | // Walks an ImageLine and records Ranges of primary and secondary colors. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | // The primary color is black and is used to denote a padding or stretching |
| 78 | // range, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 79 | // depending on which border we're iterating over. |
| 80 | // The secondary color is red and is used to denote optical bounds. |
| 81 | // |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | // An ImageLine is a templated-interface that would look something like this if |
| 83 | // it |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 84 | // were polymorphic: |
| 85 | // |
| 86 | // class ImageLine { |
| 87 | // public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | // virtual int32_t GetLength() const = 0; |
| 89 | // virtual uint32_t GetColor(int32_t idx) const = 0; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 90 | // }; |
| 91 | // |
| 92 | template <typename ImageLine> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | static bool FillRanges(const ImageLine* image_line, |
| 94 | const ColorValidator* color_validator, |
| 95 | std::vector<Range>* primary_ranges, |
| 96 | std::vector<Range>* secondary_ranges, |
| 97 | std::string* out_err) { |
| 98 | const int32_t length = image_line->GetLength(); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | uint32_t last_color = 0xffffffffu; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 101 | for (int32_t idx = 1; idx < length - 1; idx++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | const uint32_t color = image_line->GetColor(idx); |
| 103 | if (!color_validator->IsValidColor(color)) { |
| 104 | *out_err = "found an invalid color"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | return false; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 106 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | if (color != last_color) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 109 | // We are ending a range. Which range? |
| 110 | // note: encode the x offset without the final 1 pixel border. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 111 | if (last_color == kPrimaryColor) { |
| 112 | primary_ranges->back().end = idx - 1; |
| 113 | } else if (last_color == kSecondaryColor) { |
| 114 | secondary_ranges->back().end = idx - 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // We are starting a range. Which range? |
| 118 | // note: encode the x offset without the final 1 pixel border. |
| 119 | if (color == kPrimaryColor) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 120 | primary_ranges->push_back(Range(idx - 1, length - 2)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 121 | } else if (color == kSecondaryColor) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 122 | secondary_ranges->push_back(Range(idx - 1, length - 2)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | last_color = color; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | return true; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 131 | * Iterates over a row in an image. Implements the templated ImageLine |
| 132 | * interface. |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 133 | */ |
| 134 | class HorizontalImageLine { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 135 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | explicit HorizontalImageLine(uint8_t** rows, int32_t xoffset, int32_t yoffset, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 137 | int32_t length) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 138 | : rows_(rows), xoffset_(xoffset), yoffset_(yoffset), length_(length) {} |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 139 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 140 | inline int32_t GetLength() const { return length_; } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 141 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | inline uint32_t GetColor(int32_t idx) const { |
| 143 | return NinePatch::PackRGBA(rows_[yoffset_] + (idx + xoffset_) * 4); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 144 | } |
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 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 147 | uint8_t** rows_; |
| 148 | int32_t xoffset_, yoffset_, length_; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 149 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 150 | DISALLOW_COPY_AND_ASSIGN(HorizontalImageLine); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | * Iterates over a column in an image. Implements the templated ImageLine |
| 155 | * interface. |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 156 | */ |
| 157 | class VerticalImageLine { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 158 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | explicit VerticalImageLine(uint8_t** rows, int32_t xoffset, int32_t yoffset, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 160 | int32_t length) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 161 | : rows_(rows), xoffset_(xoffset), yoffset_(yoffset), length_(length) {} |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 162 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 163 | inline int32_t GetLength() const { return length_; } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 164 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 165 | inline uint32_t GetColor(int32_t idx) const { |
| 166 | return NinePatch::PackRGBA(rows_[yoffset_ + idx] + (xoffset_ * 4)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 167 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 168 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 169 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 170 | uint8_t** rows_; |
| 171 | int32_t xoffset_, yoffset_, length_; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 172 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 173 | DISALLOW_COPY_AND_ASSIGN(VerticalImageLine); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | class DiagonalImageLine { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 177 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 178 | explicit DiagonalImageLine(uint8_t** rows, int32_t xoffset, int32_t yoffset, |
| 179 | int32_t xstep, int32_t ystep, int32_t length) |
| 180 | : rows_(rows), |
| 181 | xoffset_(xoffset), |
| 182 | yoffset_(yoffset), |
| 183 | xstep_(xstep), |
| 184 | ystep_(ystep), |
| 185 | length_(length) {} |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 186 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 187 | inline int32_t GetLength() const { return length_; } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 188 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 189 | inline uint32_t GetColor(int32_t idx) const { |
| 190 | return NinePatch::PackRGBA(rows_[yoffset_ + (idx * ystep_)] + |
| 191 | ((idx + xoffset_) * xstep_) * 4); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 192 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 193 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 194 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 195 | uint8_t** rows_; |
| 196 | int32_t xoffset_, yoffset_, xstep_, ystep_, length_; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 197 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 198 | DISALLOW_COPY_AND_ASSIGN(DiagonalImageLine); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | class TransparentNeutralColorValidator : public ColorValidator { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 202 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 203 | bool IsNeutralColor(uint32_t color) const override { |
| 204 | return get_alpha(color) == 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 205 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
| 208 | class WhiteNeutralColorValidator : public ColorValidator { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | bool IsNeutralColor(uint32_t color) const override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 211 | return color == kColorOpaqueWhite; |
| 212 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 213 | }; |
| 214 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 215 | inline static uint32_t get_alpha(uint32_t color) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 216 | return (color & 0xff000000u) >> 24; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 219 | static bool PopulateBounds(const std::vector<Range>& padding, |
| 220 | const std::vector<Range>& layout_bounds, |
| 221 | const std::vector<Range>& stretch_regions, |
| 222 | const int32_t length, int32_t* padding_start, |
| 223 | int32_t* padding_end, int32_t* layout_start, |
| 224 | int32_t* layout_end, const StringPiece& edge_name, |
| 225 | std::string* out_err) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 226 | if (padding.size() > 1) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 227 | std::stringstream err_stream; |
| 228 | err_stream << "too many padding sections on " << edge_name << " border"; |
| 229 | *out_err = err_stream.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 233 | *padding_start = 0; |
| 234 | *padding_end = 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 235 | if (!padding.empty()) { |
| 236 | const Range& range = padding.front(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 237 | *padding_start = range.start; |
| 238 | *padding_end = length - range.end; |
| 239 | } else if (!stretch_regions.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 240 | // No padding was defined. Compute the padding from the first and last |
| 241 | // stretch regions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 242 | *padding_start = stretch_regions.front().start; |
| 243 | *padding_end = length - stretch_regions.back().end; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 246 | if (layout_bounds.size() > 2) { |
| 247 | std::stringstream err_stream; |
| 248 | err_stream << "too many layout bounds sections on " << edge_name |
| 249 | << " border"; |
| 250 | *out_err = err_stream.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 251 | return false; |
| 252 | } |
| 253 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 254 | *layout_start = 0; |
| 255 | *layout_end = 0; |
| 256 | if (layout_bounds.size() >= 1) { |
| 257 | const Range& range = layout_bounds.front(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 258 | // If there is only one layout bound segment, it might not start at 0, but |
| 259 | // then it should |
| 260 | // end at length. |
| 261 | if (range.start != 0 && range.end != length) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 262 | std::stringstream err_stream; |
| 263 | err_stream << "layout bounds on " << edge_name |
| 264 | << " border must start at edge"; |
| 265 | *out_err = err_stream.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 266 | return false; |
| 267 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 268 | *layout_start = range.end; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 269 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 270 | if (layout_bounds.size() >= 2) { |
| 271 | const Range& range = layout_bounds.back(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 272 | if (range.end != length) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 273 | std::stringstream err_stream; |
| 274 | err_stream << "layout bounds on " << edge_name |
| 275 | << " border must start at edge"; |
| 276 | *out_err = err_stream.str(); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 277 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 278 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 279 | *layout_end = length - range.start; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 280 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 281 | } |
| 282 | return true; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 285 | static int32_t CalculateSegmentCount(const std::vector<Range>& stretch_regions, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 286 | int32_t length) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 287 | if (stretch_regions.size() == 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 288 | return 0; |
| 289 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 290 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 291 | const bool start_is_fixed = stretch_regions.front().start != 0; |
| 292 | const bool end_is_fixed = stretch_regions.back().end != length; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 293 | int32_t modifier = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 294 | if (start_is_fixed && end_is_fixed) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 295 | modifier = 1; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | } else if (!start_is_fixed && !end_is_fixed) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 297 | modifier = -1; |
| 298 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 299 | return static_cast<int32_t>(stretch_regions.size()) * 2 + modifier; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 302 | static uint32_t GetRegionColor(uint8_t** rows, const Bounds& region) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 303 | // Sample the first pixel to compare against. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 304 | const uint32_t expected_color = |
| 305 | NinePatch::PackRGBA(rows[region.top] + region.left * 4); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 306 | for (int32_t y = region.top; y < region.bottom; y++) { |
| 307 | const uint8_t* row = rows[y]; |
| 308 | for (int32_t x = region.left; x < region.right; x++) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 309 | const uint32_t color = NinePatch::PackRGBA(row + x * 4); |
| 310 | if (get_alpha(color) == 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 311 | // The color is transparent. |
| 312 | // If the expectedColor is not transparent, NO_COLOR. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 313 | if (get_alpha(expected_color) != 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 314 | return android::Res_png_9patch::NO_COLOR; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 315 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 316 | } else if (color != expected_color) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 317 | return android::Res_png_9patch::NO_COLOR; |
| 318 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 319 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 320 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 321 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 322 | if (get_alpha(expected_color) == 0) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 323 | return android::Res_png_9patch::TRANSPARENT_COLOR; |
| 324 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 325 | return expected_color; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 328 | // Fills out_colors with each 9-patch section's color. If the whole section is |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 329 | // transparent, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 330 | // it gets the special TRANSPARENT color. If the whole section is the same |
| 331 | // color, it is assigned |
| 332 | // that color. Otherwise it gets the special NO_COLOR color. |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 333 | // |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 334 | // Note that the rows contain the 9-patch 1px border, and the indices in the |
| 335 | // stretch regions are |
| 336 | // already offset to exclude the border. This means that each time the rows are |
| 337 | // accessed, |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 338 | // the indices must be offset by 1. |
| 339 | // |
| 340 | // width and height also include the 9-patch 1px border. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 341 | static void CalculateRegionColors( |
| 342 | uint8_t** rows, const std::vector<Range>& horizontal_stretch_regions, |
| 343 | const std::vector<Range>& vertical_stretch_regions, const int32_t width, |
| 344 | const int32_t height, std::vector<uint32_t>* out_colors) { |
| 345 | int32_t next_top = 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 346 | Bounds bounds; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 347 | auto row_iter = vertical_stretch_regions.begin(); |
| 348 | while (next_top != height) { |
| 349 | if (row_iter != vertical_stretch_regions.end()) { |
| 350 | if (next_top != row_iter->start) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 351 | // This is a fixed segment. |
| 352 | // Offset the bounds by 1 to accommodate the border. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 353 | bounds.top = next_top + 1; |
| 354 | bounds.bottom = row_iter->start + 1; |
| 355 | next_top = row_iter->start; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 356 | } else { |
| 357 | // This is a stretchy segment. |
| 358 | // Offset the bounds by 1 to accommodate the border. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 359 | bounds.top = row_iter->start + 1; |
| 360 | bounds.bottom = row_iter->end + 1; |
| 361 | next_top = row_iter->end; |
| 362 | ++row_iter; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 363 | } |
| 364 | } else { |
| 365 | // This is the end, fixed section. |
| 366 | // Offset the bounds by 1 to accommodate the border. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 367 | bounds.top = next_top + 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 368 | bounds.bottom = height + 1; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 369 | next_top = height; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 370 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 371 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 372 | int32_t next_left = 0; |
| 373 | auto col_iter = horizontal_stretch_regions.begin(); |
| 374 | while (next_left != width) { |
| 375 | if (col_iter != horizontal_stretch_regions.end()) { |
| 376 | if (next_left != col_iter->start) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 377 | // This is a fixed segment. |
| 378 | // Offset the bounds by 1 to accommodate the border. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 379 | bounds.left = next_left + 1; |
| 380 | bounds.right = col_iter->start + 1; |
| 381 | next_left = col_iter->start; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 382 | } else { |
| 383 | // This is a stretchy segment. |
| 384 | // Offset the bounds by 1 to accommodate the border. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 385 | bounds.left = col_iter->start + 1; |
| 386 | bounds.right = col_iter->end + 1; |
| 387 | next_left = col_iter->end; |
| 388 | ++col_iter; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 389 | } |
| 390 | } else { |
| 391 | // This is the end, fixed section. |
| 392 | // Offset the bounds by 1 to accommodate the border. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 393 | bounds.left = next_left + 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 394 | bounds.right = width + 1; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 395 | next_left = width; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 396 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 397 | out_colors->push_back(GetRegionColor(rows, bounds)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 402 | // Calculates the insets of a row/column of pixels based on where the largest |
| 403 | // alpha value begins |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 404 | // (on both sides). |
| 405 | template <typename ImageLine> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 406 | static void FindOutlineInsets(const ImageLine* image_line, int32_t* out_start, |
| 407 | int32_t* out_end) { |
| 408 | *out_start = 0; |
| 409 | *out_end = 0; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 410 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 411 | const int32_t length = image_line->GetLength(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 412 | if (length < 3) { |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 413 | return; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // If the length is odd, we want both sides to process the center pixel, |
| 417 | // so we use two different midpoints (to account for < and <= in the different |
| 418 | // loops). |
| 419 | const int32_t mid2 = length / 2; |
| 420 | const int32_t mid1 = mid2 + (length % 2); |
| 421 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 422 | uint32_t max_alpha = 0; |
| 423 | for (int32_t i = 0; i < mid1 && max_alpha != 0xff; i++) { |
| 424 | uint32_t alpha = get_alpha(image_line->GetColor(i)); |
| 425 | if (alpha > max_alpha) { |
| 426 | max_alpha = alpha; |
| 427 | *out_start = i; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 431 | max_alpha = 0; |
| 432 | for (int32_t i = length - 1; i >= mid2 && max_alpha != 0xff; i--) { |
| 433 | uint32_t alpha = get_alpha(image_line->GetColor(i)); |
| 434 | if (alpha > max_alpha) { |
| 435 | max_alpha = alpha; |
| 436 | *out_end = length - (i + 1); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | return; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | template <typename ImageLine> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 443 | static uint32_t FindMaxAlpha(const ImageLine* image_line) { |
| 444 | const int32_t length = image_line->GetLength(); |
| 445 | uint32_t max_alpha = 0; |
| 446 | for (int32_t idx = 0; idx < length && max_alpha != 0xff; idx++) { |
| 447 | uint32_t alpha = get_alpha(image_line->GetColor(idx)); |
| 448 | if (alpha > max_alpha) { |
| 449 | max_alpha = alpha; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 450 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 451 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 452 | return max_alpha; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | // Pack the pixels in as 0xAARRGGBB (as 9-patch expects it). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 456 | uint32_t NinePatch::PackRGBA(const uint8_t* pixel) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 457 | return (pixel[3] << 24) | (pixel[0] << 16) | (pixel[1] << 8) | pixel[2]; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 460 | std::unique_ptr<NinePatch> NinePatch::Create(uint8_t** rows, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 461 | const int32_t width, |
| 462 | const int32_t height, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 463 | std::string* out_err) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 464 | if (width < 3 || height < 3) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 465 | *out_err = "image must be at least 3x3 (1x1 image with 1 pixel border)"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 466 | return {}; |
| 467 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 468 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 469 | std::vector<Range> horizontal_padding; |
| 470 | std::vector<Range> horizontal_layout_bounds; |
| 471 | std::vector<Range> vertical_padding; |
| 472 | std::vector<Range> vertical_layout_bounds; |
| 473 | std::vector<Range> unexpected_ranges; |
| 474 | std::unique_ptr<ColorValidator> color_validator; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 475 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 476 | if (rows[0][3] == 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 477 | color_validator = util::make_unique<TransparentNeutralColorValidator>(); |
| 478 | } else if (PackRGBA(rows[0]) == kColorOpaqueWhite) { |
| 479 | color_validator = util::make_unique<WhiteNeutralColorValidator>(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 480 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 481 | *out_err = |
| 482 | "top-left corner pixel must be either opaque white or transparent"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 483 | return {}; |
| 484 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 485 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 486 | // Private constructor, can't use make_unique. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 487 | auto nine_patch = std::unique_ptr<NinePatch>(new NinePatch()); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 488 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 489 | HorizontalImageLine top_row(rows, 0, 0, width); |
| 490 | if (!FillRanges(&top_row, color_validator.get(), |
| 491 | &nine_patch->horizontal_stretch_regions, &unexpected_ranges, |
| 492 | out_err)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 493 | return {}; |
| 494 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 495 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 496 | if (!unexpected_ranges.empty()) { |
| 497 | const Range& range = unexpected_ranges[0]; |
| 498 | std::stringstream err_stream; |
| 499 | err_stream << "found unexpected optical bounds (red pixel) on top border " |
| 500 | << "at x=" << range.start + 1; |
| 501 | *out_err = err_stream.str(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 502 | return {}; |
| 503 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 504 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 505 | VerticalImageLine left_col(rows, 0, 0, height); |
| 506 | if (!FillRanges(&left_col, color_validator.get(), |
| 507 | &nine_patch->vertical_stretch_regions, &unexpected_ranges, |
| 508 | out_err)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 509 | return {}; |
| 510 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 511 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 512 | if (!unexpected_ranges.empty()) { |
| 513 | const Range& range = unexpected_ranges[0]; |
| 514 | std::stringstream err_stream; |
| 515 | err_stream << "found unexpected optical bounds (red pixel) on left border " |
| 516 | << "at y=" << range.start + 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 517 | return {}; |
| 518 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 519 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 520 | HorizontalImageLine bottom_row(rows, 0, height - 1, width); |
| 521 | if (!FillRanges(&bottom_row, color_validator.get(), &horizontal_padding, |
| 522 | &horizontal_layout_bounds, out_err)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 523 | return {}; |
| 524 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 525 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 526 | if (!PopulateBounds(horizontal_padding, horizontal_layout_bounds, |
| 527 | nine_patch->horizontal_stretch_regions, width - 2, |
| 528 | &nine_patch->padding.left, &nine_patch->padding.right, |
| 529 | &nine_patch->layout_bounds.left, |
| 530 | &nine_patch->layout_bounds.right, "bottom", out_err)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 531 | return {}; |
| 532 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 533 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 534 | VerticalImageLine right_col(rows, width - 1, 0, height); |
| 535 | if (!FillRanges(&right_col, color_validator.get(), &vertical_padding, |
| 536 | &vertical_layout_bounds, out_err)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 537 | return {}; |
| 538 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 539 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 540 | if (!PopulateBounds(vertical_padding, vertical_layout_bounds, |
| 541 | nine_patch->vertical_stretch_regions, height - 2, |
| 542 | &nine_patch->padding.top, &nine_patch->padding.bottom, |
| 543 | &nine_patch->layout_bounds.top, |
| 544 | &nine_patch->layout_bounds.bottom, "right", out_err)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 545 | return {}; |
| 546 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 547 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 548 | // Fill the region colors of the 9-patch. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 549 | const int32_t num_rows = |
| 550 | CalculateSegmentCount(nine_patch->horizontal_stretch_regions, width - 2); |
| 551 | const int32_t num_cols = |
| 552 | CalculateSegmentCount(nine_patch->vertical_stretch_regions, height - 2); |
| 553 | if ((int64_t)num_rows * (int64_t)num_cols > 0x7f) { |
| 554 | *out_err = "too many regions in 9-patch"; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 555 | return {}; |
| 556 | } |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 557 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 558 | nine_patch->region_colors.reserve(num_rows * num_cols); |
| 559 | CalculateRegionColors(rows, nine_patch->horizontal_stretch_regions, |
| 560 | nine_patch->vertical_stretch_regions, width - 2, |
| 561 | height - 2, &nine_patch->region_colors); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 562 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 563 | // Compute the outline based on opacity. |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 564 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 565 | // Find left and right extent of 9-patch content on center row. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 566 | HorizontalImageLine mid_row(rows, 1, height / 2, width - 2); |
| 567 | FindOutlineInsets(&mid_row, &nine_patch->outline.left, |
| 568 | &nine_patch->outline.right); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 569 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 570 | // Find top and bottom extent of 9-patch content on center column. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 571 | VerticalImageLine mid_col(rows, width / 2, 1, height - 2); |
| 572 | FindOutlineInsets(&mid_col, &nine_patch->outline.top, |
| 573 | &nine_patch->outline.bottom); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 574 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 575 | const int32_t outline_width = |
| 576 | (width - 2) - nine_patch->outline.left - nine_patch->outline.right; |
| 577 | const int32_t outline_height = |
| 578 | (height - 2) - nine_patch->outline.top - nine_patch->outline.bottom; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 579 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 580 | // Find the largest alpha value within the outline area. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 581 | HorizontalImageLine outline_mid_row( |
| 582 | rows, 1 + nine_patch->outline.left, |
| 583 | 1 + nine_patch->outline.top + (outline_height / 2), outline_width); |
| 584 | VerticalImageLine outline_mid_col( |
| 585 | rows, 1 + nine_patch->outline.left + (outline_width / 2), |
| 586 | 1 + nine_patch->outline.top, outline_height); |
| 587 | nine_patch->outline_alpha = |
| 588 | std::max(FindMaxAlpha(&outline_mid_row), FindMaxAlpha(&outline_mid_col)); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 589 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 590 | // Assuming the image is a round rect, compute the radius by marching |
| 591 | // diagonally from the top left corner towards the center. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 592 | DiagonalImageLine diagonal(rows, 1 + nine_patch->outline.left, |
| 593 | 1 + nine_patch->outline.top, 1, 1, |
| 594 | std::min(outline_width, outline_height)); |
| 595 | int32_t top_left, bottom_right; |
| 596 | FindOutlineInsets(&diagonal, &top_left, &bottom_right); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 597 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 598 | /* Determine source radius based upon inset: |
| 599 | * sqrt(r^2 + r^2) = sqrt(i^2 + i^2) + r |
| 600 | * sqrt(2) * r = sqrt(2) * i + r |
| 601 | * (sqrt(2) - 1) * r = sqrt(2) * i |
| 602 | * r = sqrt(2) / (sqrt(2) - 1) * i |
| 603 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 604 | nine_patch->outline_radius = 3.4142f * top_left; |
| 605 | return nine_patch; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 608 | std::unique_ptr<uint8_t[]> NinePatch::SerializeBase(size_t* outLen) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 609 | android::Res_png_9patch data; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 610 | data.numXDivs = static_cast<uint8_t>(horizontal_stretch_regions.size()) * 2; |
| 611 | data.numYDivs = static_cast<uint8_t>(vertical_stretch_regions.size()) * 2; |
| 612 | data.numColors = static_cast<uint8_t>(region_colors.size()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 613 | data.paddingLeft = padding.left; |
| 614 | data.paddingRight = padding.right; |
| 615 | data.paddingTop = padding.top; |
| 616 | data.paddingBottom = padding.bottom; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 617 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 618 | auto buffer = std::unique_ptr<uint8_t[]>(new uint8_t[data.serializedSize()]); |
| 619 | android::Res_png_9patch::serialize( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 620 | data, (const int32_t*)horizontal_stretch_regions.data(), |
| 621 | (const int32_t*)vertical_stretch_regions.data(), region_colors.data(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 622 | buffer.get()); |
| 623 | // Convert to file endianness. |
| 624 | reinterpret_cast<android::Res_png_9patch*>(buffer.get())->deviceToFile(); |
Adam Lesinski | edba941 | 2016-10-04 17:33:04 -0700 | [diff] [blame] | 625 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 626 | *outLen = data.serializedSize(); |
| 627 | return buffer; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 630 | std::unique_ptr<uint8_t[]> NinePatch::SerializeLayoutBounds( |
| 631 | size_t* out_len) const { |
| 632 | size_t chunk_len = sizeof(uint32_t) * 4; |
| 633 | auto buffer = std::unique_ptr<uint8_t[]>(new uint8_t[chunk_len]); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 634 | uint8_t* cursor = buffer.get(); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 635 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 636 | memcpy(cursor, &layout_bounds.left, sizeof(layout_bounds.left)); |
| 637 | cursor += sizeof(layout_bounds.left); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 638 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 639 | memcpy(cursor, &layout_bounds.top, sizeof(layout_bounds.top)); |
| 640 | cursor += sizeof(layout_bounds.top); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 641 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 642 | memcpy(cursor, &layout_bounds.right, sizeof(layout_bounds.right)); |
| 643 | cursor += sizeof(layout_bounds.right); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 644 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 645 | memcpy(cursor, &layout_bounds.bottom, sizeof(layout_bounds.bottom)); |
| 646 | cursor += sizeof(layout_bounds.bottom); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 647 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 648 | *out_len = chunk_len; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 649 | return buffer; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 652 | std::unique_ptr<uint8_t[]> NinePatch::SerializeRoundedRectOutline( |
| 653 | size_t* out_len) const { |
| 654 | size_t chunk_len = sizeof(uint32_t) * 6; |
| 655 | auto buffer = std::unique_ptr<uint8_t[]>(new uint8_t[chunk_len]); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 656 | uint8_t* cursor = buffer.get(); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 657 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 658 | memcpy(cursor, &outline.left, sizeof(outline.left)); |
| 659 | cursor += sizeof(outline.left); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 660 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 661 | memcpy(cursor, &outline.top, sizeof(outline.top)); |
| 662 | cursor += sizeof(outline.top); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 663 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 664 | memcpy(cursor, &outline.right, sizeof(outline.right)); |
| 665 | cursor += sizeof(outline.right); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 666 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 667 | memcpy(cursor, &outline.bottom, sizeof(outline.bottom)); |
| 668 | cursor += sizeof(outline.bottom); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 669 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 670 | *((float*)cursor) = outline_radius; |
| 671 | cursor += sizeof(outline_radius); |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 672 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 673 | *((uint32_t*)cursor) = outline_alpha; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 674 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 675 | *out_len = chunk_len; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 676 | return buffer; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | ::std::ostream& operator<<(::std::ostream& out, const Range& range) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 680 | return out << "[" << range.start << ", " << range.end << ")"; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | ::std::ostream& operator<<(::std::ostream& out, const Bounds& bounds) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 684 | return out << "l=" << bounds.left << " t=" << bounds.top |
| 685 | << " r=" << bounds.right << " b=" << bounds.bottom; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 688 | ::std::ostream& operator<<(::std::ostream& out, const NinePatch& nine_patch) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 689 | return out << "horizontalStretch:" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 690 | << util::Joiner(nine_patch.horizontal_stretch_regions, " ") |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 691 | << " verticalStretch:" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 692 | << util::Joiner(nine_patch.vertical_stretch_regions, " ") |
| 693 | << " padding: " << nine_patch.padding |
| 694 | << ", bounds: " << nine_patch.layout_bounds |
| 695 | << ", outline: " << nine_patch.outline |
| 696 | << " rad=" << nine_patch.outline_radius |
| 697 | << " alpha=" << nine_patch.outline_alpha; |
Adam Lesinski | 21efb68 | 2016-09-14 17:35:43 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 700 | } // namespace aapt |