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