1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.formats.webp.chunks;
18
19 import java.io.IOException;
20 import java.io.PrintWriter;
21
22 import org.apache.commons.imaging.ImagingException;
23 import org.apache.commons.imaging.internal.SafeOperations;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public final class WebPChunkVp8l extends WebPChunk {
43 private final int imageWidth;
44 private final int imageHeight;
45 private final boolean hasAlpha;
46 private final int versionNumber;
47
48
49
50
51
52
53
54
55
56 public WebPChunkVp8l(final int type, final int size, final byte[] bytes) throws ImagingException {
57 super(type, size, bytes);
58
59 if (bytes[0] != 0x2f || size < 5) {
60 throw new ImagingException("Invalid VP8L chunk");
61 }
62
63 final int b1 = bytes[1] & 0xFF;
64 final int b2 = bytes[2] & 0xFF;
65 final int b3 = bytes[3] & 0xFF;
66 final int b4 = bytes[4] & 0xFF;
67
68 this.imageWidth = b1 + ((b2 & 0b0011_1111) << 8) + 1;
69 this.imageHeight = SafeOperations.add((b2 & 0b1100_0000) >> 6, b3 << 2, (b4 & 0b1111) << 8, 1);
70 this.hasAlpha = (b4 & 0b0001_0000) != 0;
71 this.versionNumber = b4 >> 5;
72
73 if (versionNumber != 0) {
74 throw new ImagingException("VP8L version should be 0");
75 }
76 }
77
78 @Override
79 public void dump(final PrintWriter pw, final int offset) throws ImagingException, IOException {
80 super.dump(pw, offset);
81 pw.println(" Version Number: " + getVersionNumber());
82 pw.println(" Image Width: " + getImageWidth());
83 pw.println(" Image Height: " + getImageHeight());
84 pw.println(" Alpha: " + hasAlpha());
85 }
86
87
88
89
90 public int getImageHeight() {
91 return imageHeight;
92 }
93
94
95
96
97 public int getImageWidth() {
98 return imageWidth;
99 }
100
101
102
103
104 public int getVersionNumber() {
105 return versionNumber;
106 }
107
108
109
110
111 public boolean hasAlpha() {
112 return hasAlpha;
113 }
114 }