File: Image.h

package info (click to toggle)
android-platform-frameworks-base 1%3A10.0.0%2Br36-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 321,788 kB
  • sloc: java: 962,234; cpp: 274,314; xml: 242,770; python: 5,060; sh: 1,432; ansic: 494; makefile: 47; sed: 19
file content (209 lines) | stat: -rw-r--r-- 5,723 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef AAPT_COMPILE_IMAGE_H
#define AAPT_COMPILE_IMAGE_H

#include <cstdint>
#include <memory>
#include <string>
#include <vector>

#include "android-base/macros.h"

namespace aapt {

/**
 * An in-memory image, loaded from disk, with pixels in RGBA_8888 format.
 */
class Image {
 public:
  explicit Image() = default;

  /**
   * A `height` sized array of pointers, where each element points to a
   * `width` sized row of RGBA_8888 pixels.
   */
  std::unique_ptr<uint8_t* []> rows;

  /**
   * The width of the image in RGBA_8888 pixels. This is int32_t because of
   * 9-patch data
   * format limitations.
   */
  int32_t width = 0;

  /**
   * The height of the image in RGBA_8888 pixels. This is int32_t because of
   * 9-patch data
   * format limitations.
   */
  int32_t height = 0;

  /**
   * Buffer to the raw image data stored sequentially.
   * Use `rows` to access the data on a row-by-row basis.
   */
  std::unique_ptr<uint8_t[]> data;

 private:
  DISALLOW_COPY_AND_ASSIGN(Image);
};

/**
 * A range of pixel values, starting at 'start' and ending before 'end'
 * exclusive. Or rather [a, b).
 */
struct Range {
  int32_t start = 0;
  int32_t end = 0;

  explicit Range() = default;
  inline explicit Range(int32_t s, int32_t e) : start(s), end(e) {}
};

inline bool operator==(const Range& left, const Range& right) {
  return left.start == right.start && left.end == right.end;
}

/**
 * Inset lengths from all edges of a rectangle. `left` and `top` are measured
 * from the left and top
 * edges, while `right` and `bottom` are measured from the right and bottom
 * edges, respectively.
 */
struct Bounds {
  int32_t left = 0;
  int32_t top = 0;
  int32_t right = 0;
  int32_t bottom = 0;

  explicit Bounds() = default;
  inline explicit Bounds(int32_t l, int32_t t, int32_t r, int32_t b)
      : left(l), top(t), right(r), bottom(b) {}

  bool nonZero() const;
};

inline bool Bounds::nonZero() const {
  return left != 0 || top != 0 || right != 0 || bottom != 0;
}

inline bool operator==(const Bounds& left, const Bounds& right) {
  return left.left == right.left && left.top == right.top &&
         left.right == right.right && left.bottom == right.bottom;
}

/**
 * Contains 9-patch data from a source image. All measurements exclude the 1px
 * border of the
 * source 9-patch image.
 */
class NinePatch {
 public:
  static std::unique_ptr<NinePatch> Create(uint8_t** rows, const int32_t width,
                                           const int32_t height,
                                           std::string* err_out);

  /**
   * Packs the RGBA_8888 data pointed to by pixel into a uint32_t
   * with format 0xAARRGGBB (the way 9-patch expects it).
   */
  static uint32_t PackRGBA(const uint8_t* pixel);

  /**
   * 9-patch content padding/insets. All positions are relative to the 9-patch
   * NOT including the 1px thick source border.
   */
  Bounds padding;

  /**
   * Optical layout bounds/insets. This overrides the padding for
   * layout purposes. All positions are relative to the 9-patch
   * NOT including the 1px thick source border.
   * See
   * https://developer.android.com/about/versions/android-4.3.html#OpticalBounds
   */
  Bounds layout_bounds;

  /**
   * Outline of the image, calculated based on opacity.
   */
  Bounds outline;

  /**
   * The computed radius of the outline. If non-zero, the outline is a
   * rounded-rect.
   */
  float outline_radius = 0.0f;

  /**
   * The largest alpha value within the outline.
   */
  uint32_t outline_alpha = 0x000000ffu;

  /**
   * Horizontal regions of the image that are stretchable.
   * All positions are relative to the 9-patch
   * NOT including the 1px thick source border.
   */
  std::vector<Range> horizontal_stretch_regions;

  /**
   * Vertical regions of the image that are stretchable.
   * All positions are relative to the 9-patch
   * NOT including the 1px thick source border.
   */
  std::vector<Range> vertical_stretch_regions;

  /**
   * The colors within each region, fixed or stretchable.
   * For w*h regions, the color of region (x,y) is addressable
   * via index y*w + x.
   */
  std::vector<uint32_t> region_colors;

  /**
   * Returns serialized data containing the original basic 9-patch meta data.
   * Optical layout bounds and round rect outline data must be serialized
   * separately using SerializeOpticalLayoutBounds() and
   * SerializeRoundedRectOutline().
   */
  std::unique_ptr<uint8_t[]> SerializeBase(size_t* out_len) const;

  /**
   * Serializes the layout bounds.
   */
  std::unique_ptr<uint8_t[]> SerializeLayoutBounds(size_t* out_len) const;

  /**
   * Serializes the rounded-rect outline.
   */
  std::unique_ptr<uint8_t[]> SerializeRoundedRectOutline(size_t* out_len) const;

 private:
  explicit NinePatch() = default;

  DISALLOW_COPY_AND_ASSIGN(NinePatch);
};

::std::ostream& operator<<(::std::ostream& out, const Range& range);
::std::ostream& operator<<(::std::ostream& out, const Bounds& bounds);
::std::ostream& operator<<(::std::ostream& out, const NinePatch& nine_patch);

}  // namespace aapt

#endif /* AAPT_COMPILE_IMAGE_H */