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