Debian Codegraph @

brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/Res9patchStreamDecoder.java

raw
1 /*
2 * Copyright (C) 2010 Ryszard Wiśniewski <[email protected]>
3 * Copyright (C) 2010 Connor Tumbleson <[email protected]>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package brut.androlib.res.decoder;
18
19 import brut.androlib.AndrolibException;
20 import brut.androlib.err.CantFind9PatchChunkException;
21 import brut.util.ExtDataInput;
22 import org.apache.commons.io.IOUtils;
23
24 import javax.imageio.ImageIO;
25 import java.awt.image.BufferedImage;
26 import java.awt.image.Raster;
27 import java.awt.image.WritableRaster;
28 import java.io.ByteArrayInputStream;
29 import java.io.DataInput;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33
34 public class Res9patchStreamDecoder implements ResStreamDecoder {
35 @Override
36 public void decode(InputStream in, OutputStream out) throws AndrolibException {
37 try {
38 byte[] data = IOUtils.toByteArray(in);
39
40 if (data.length == 0) {
41 return;
42 }
43
44 BufferedImage im = ImageIO.read(new ByteArrayInputStream(data));
45 int w = im.getWidth(), h = im.getHeight();
46
47 BufferedImage im2 = new BufferedImage(w + 2, h + 2, BufferedImage.TYPE_INT_ARGB);
48 if (im.getType() == BufferedImage.TYPE_CUSTOM) {
49 //TODO: Ensure this is gray + alpha case?
50 Raster srcRaster = im.getRaster();
51 WritableRaster dstRaster = im2.getRaster();
52 int[] gray = null, alpha = null;
53 for (int y = 0; y < im.getHeight(); y++) {
54 gray = srcRaster.getSamples(0, y, w, 1, 0, gray);
55 alpha = srcRaster.getSamples(0, y, w, 1, 1, alpha);
56
57 dstRaster.setSamples(1, y + 1, w, 1, 0, gray);
58 dstRaster.setSamples(1, y + 1, w, 1, 1, gray);
59 dstRaster.setSamples(1, y + 1, w, 1, 2, gray);
60 dstRaster.setSamples(1, y + 1, w, 1, 3, alpha);
61 }
62 } else {
63 im2.createGraphics().drawImage(im, 1, 1, w, h, null);
64 }
65
66 NinePatch np = getNinePatch(data);
67 drawHLine(im2, h + 1, np.padLeft + 1, w - np.padRight);
68 drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom);
69
70 int[] xDivs = np.xDivs;
71 if (xDivs.length == 0) {
72 drawHLine(im2, 0, 1, w);
73 } else {
74 for (int i = 0; i < xDivs.length; i += 2) {
75 drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]);
76 }
77 }
78
79 int[] yDivs = np.yDivs;
80 if (yDivs.length == 0) {
81 drawVLine(im2, 0, 1, h);
82 } else {
83 for (int i = 0; i < yDivs.length; i += 2) {
84 drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]);
85 }
86 }
87
88 // Some images additionally use Optical Bounds
89 // https://developer.android.com/about/versions/android-4.3.html#OpticalBounds
90 try {
91 OpticalInset oi = getOpticalInset(data);
92
93 for (int i = 0; i < oi.layoutBoundsLeft; i++) {
94 int x = 1 + i;
95 im2.setRGB(x, h + 1, OI_COLOR);
96 }
97
98 for (int i = 0; i < oi.layoutBoundsRight; i++) {
99 int x = w - i;
100 im2.setRGB(x, h + 1, OI_COLOR);
101 }
102
103 for (int i = 0; i < oi.layoutBoundsTop; i++) {
104 int y = 1 + i;
105 im2.setRGB(w + 1, y, OI_COLOR);
106 }
107
108 for (int i = 0; i < oi.layoutBoundsBottom; i++) {
109 int y = h - i;
110 im2.setRGB(w + 1, y, OI_COLOR);
111 }
112 } catch (CantFind9PatchChunkException t) {
113 // This chunk might not exist
114 }
115
116 ImageIO.write(im2, "png", out);
117 } catch (IOException | NullPointerException ex) {
118 // In my case this was triggered because a .png file was
119 // containing a html document instead of an image.
120 // This could be more verbose and try to MIME ?
121 throw new AndrolibException(ex);
122 }
123 }
124
125 private NinePatch getNinePatch(byte[] data) throws AndrolibException,
126 IOException {
127 ExtDataInput di = new ExtDataInput(new ByteArrayInputStream(data));
128 find9patchChunk(di, NP_CHUNK_TYPE);
129 return NinePatch.decode(di);
130 }
131
132 private OpticalInset getOpticalInset(byte[] data) throws AndrolibException,
133 IOException {
134 ExtDataInput di = new ExtDataInput(new ByteArrayInputStream(data));
135 find9patchChunk(di, OI_CHUNK_TYPE);
136 return OpticalInset.decode(di);
137 }
138
139 private void find9patchChunk(DataInput di, int magic) throws AndrolibException,
140 IOException {
141 di.skipBytes(8);
142 while (true) {
143 int size;
144 try {
145 size = di.readInt();
146 } catch (IOException ex) {
147 throw new CantFind9PatchChunkException("Cant find nine patch chunk", ex);
148 }
149 if (di.readInt() == magic) {
150 return;
151 }
152 di.skipBytes(size + 4);
153 }
154 }
155
156 private void drawHLine(BufferedImage im, int y, int x1, int x2) {
157 for (int x = x1; x <= x2; x++) {
158 im.setRGB(x, y, NP_COLOR);
159 }
160 }
161
162 private void drawVLine(BufferedImage im, int x, int y1, int y2) {
163 for (int y = y1; y <= y2; y++) {
164 im.setRGB(x, y, NP_COLOR);
165 }
166 }
167
168 private static final int NP_CHUNK_TYPE = 0x6e705463; // npTc
169 private static final int OI_CHUNK_TYPE = 0x6e704c62; // npLb
170 private static final int NP_COLOR = 0xff000000;
171 private static final int OI_COLOR = 0xffff0000;
172
173 private static class NinePatch {
174 public final int padLeft, padRight, padTop, padBottom;
175 public final int[] xDivs, yDivs;
176
177 public NinePatch(int padLeft, int padRight, int padTop, int padBottom,
178 int[] xDivs, int[] yDivs) {
179 this.padLeft = padLeft;
180 this.padRight = padRight;
181 this.padTop = padTop;
182 this.padBottom = padBottom;
183 this.xDivs = xDivs;
184 this.yDivs = yDivs;
185 }
186
187 public static NinePatch decode(ExtDataInput di) throws IOException {
188 di.skipBytes(1); // wasDeserialized
189 byte numXDivs = di.readByte();
190 byte numYDivs = di.readByte();
191 di.skipBytes(1); // numColors
192 di.skipBytes(8); // xDivs/yDivs offset
193 int padLeft = di.readInt();
194 int padRight = di.readInt();
195 int padTop = di.readInt();
196 int padBottom = di.readInt();
197 di.skipBytes(4); // colorsOffset
198 int[] xDivs = di.readIntArray(numXDivs);
199 int[] yDivs = di.readIntArray(numYDivs);
200
201 return new NinePatch(padLeft, padRight, padTop, padBottom, xDivs, yDivs);
202 }
203 }
204
205 private static class OpticalInset {
206 public final int layoutBoundsLeft, layoutBoundsTop, layoutBoundsRight, layoutBoundsBottom;
207
208 public OpticalInset(int layoutBoundsLeft, int layoutBoundsTop,
209 int layoutBoundsRight, int layoutBoundsBottom) {
210 this.layoutBoundsLeft = layoutBoundsLeft;
211 this.layoutBoundsTop = layoutBoundsTop;
212 this.layoutBoundsRight = layoutBoundsRight;
213 this.layoutBoundsBottom = layoutBoundsBottom;
214 }
215
216 public static OpticalInset decode(ExtDataInput di) throws IOException {
217 int layoutBoundsLeft = Integer.reverseBytes(di.readInt());
218 int layoutBoundsTop = Integer.reverseBytes(di.readInt());
219 int layoutBoundsRight = Integer.reverseBytes(di.readInt());
220 int layoutBoundsBottom = Integer.reverseBytes(di.readInt());
221 return new OpticalInset(layoutBoundsLeft, layoutBoundsTop,
222 layoutBoundsRight, layoutBoundsBottom);
223 }
224 }
225 }