-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQOIEncoder.java
151 lines (144 loc) · 4.63 KB
/
QOIEncoder.java
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
// Generated automatically with "fut". Do not edit.
import java.util.Arrays;
/**
* Encoder of the "Quite OK Image" (QOI) format.
* Losslessly compresses an image to a byte array.
*/
public class QOIEncoder
{
/**
* Constructs the encoder.
* The encoder can be used for several images, one after another.
*/
public QOIEncoder()
{
}
static final int HEADER_SIZE = 14;
static final int PADDING_SIZE = 8;
private byte[] encoded;
private int encodedSize;
/**
* Determines if an image of given size can be encoded.
* @param width Image width in pixels.
* @param height Image height in pixels.
* @param alpha Whether the image has the alpha channel (transparency).
*/
public static boolean canEncode(int width, int height, boolean alpha)
{
return width > 0 && height > 0 && height <= 2147483625 / width / (alpha ? 5 : 4);
}
/**
* Encodes the given image.
* Returns <code>true</code> if encoded successfully.
* @param width Image width in pixels.
* @param height Image height in pixels.
* @param pixels Pixels of the image, top-down, left-to-right.
* @param alpha <code>false</code> specifies that all pixels are opaque. High bytes of <code>pixels</code> elements are ignored then.
* @param linearColorspace Specifies the color space.
*/
public final boolean encode(int width, int height, int[] pixels, boolean alpha, boolean linearColorspace)
{
if (!canEncode(width, height, alpha))
return false;
int pixelsSize = width * height;
byte[] encoded = new byte[14 + pixelsSize * (alpha ? 5 : 4) + 8];
encoded[0] = 'q';
encoded[1] = 'o';
encoded[2] = 'i';
encoded[3] = 'f';
encoded[4] = (byte) (width >> 24);
encoded[5] = (byte) (width >> 16);
encoded[6] = (byte) (width >> 8);
encoded[7] = (byte) width;
encoded[8] = (byte) (height >> 24);
encoded[9] = (byte) (height >> 16);
encoded[10] = (byte) (height >> 8);
encoded[11] = (byte) height;
encoded[12] = (byte) (alpha ? 4 : 3);
encoded[13] = (byte) (linearColorspace ? 1 : 0);
final int[] index = new int[64];
int encodedOffset = 14;
int lastPixel = -16777216;
int run = 0;
for (int pixelsOffset = 0; pixelsOffset < pixelsSize;) {
int pixel = pixels[pixelsOffset++];
if (!alpha)
pixel |= -16777216;
if (pixel == lastPixel) {
if (++run == 62 || pixelsOffset >= pixelsSize) {
encoded[encodedOffset++] = (byte) (191 + run);
run = 0;
}
}
else {
if (run > 0) {
encoded[encodedOffset++] = (byte) (191 + run);
run = 0;
}
int indexOffset = ((pixel >> 16) * 3 + (pixel >> 8) * 5 + (pixel & 63) * 7 + (pixel >> 24) * 11) & 63;
if (pixel == index[indexOffset])
encoded[encodedOffset++] = (byte) indexOffset;
else {
index[indexOffset] = pixel;
int r = pixel >> 16 & 255;
int g = pixel >> 8 & 255;
int b = pixel & 255;
if ((pixel ^ lastPixel) >> 24 != 0) {
encoded[encodedOffset] = (byte) 255;
encoded[encodedOffset + 1] = (byte) r;
encoded[encodedOffset + 2] = (byte) g;
encoded[encodedOffset + 3] = (byte) b;
encoded[encodedOffset + 4] = (byte) (pixel >> 24);
encodedOffset += 5;
}
else {
int dr = (((r - (lastPixel >> 16)) & 255) ^ 128) - 128;
int dg = (((g - (lastPixel >> 8)) & 255) ^ 128) - 128;
int db = (((b - lastPixel) & 255) ^ 128) - 128;
if (dr >= -2 && dr <= 1 && dg >= -2 && dg <= 1 && db >= -2 && db <= 1)
encoded[encodedOffset++] = (byte) (106 + (dr << 4) + (dg << 2) + db);
else {
dr -= dg;
db -= dg;
if (dr >= -8 && dr <= 7 && dg >= -32 && dg <= 31 && db >= -8 && db <= 7) {
encoded[encodedOffset] = (byte) (160 + dg);
encoded[encodedOffset + 1] = (byte) (136 + (dr << 4) + db);
encodedOffset += 2;
}
else {
encoded[encodedOffset] = (byte) 254;
encoded[encodedOffset + 1] = (byte) r;
encoded[encodedOffset + 2] = (byte) g;
encoded[encodedOffset + 3] = (byte) b;
encodedOffset += 4;
}
}
}
}
lastPixel = pixel;
}
}
Arrays.fill(encoded, encodedOffset, encodedOffset + 7, (byte) 0);
encoded[encodedOffset + 8 - 1] = 1;
this.encoded = encoded;
this.encodedSize = encodedOffset + 8;
return true;
}
/**
* Returns the encoded file contents.
* This method can only be called after <code>Encode</code> returned <code>true</code>.
* The allocated array is usually larger than the encoded data.
* Call <code>GetEncodedSize</code> to retrieve the number of leading bytes that are significant.
*/
public final byte[] getEncoded()
{
return this.encoded;
}
/**
* Returns the encoded file length.
*/
public final int getEncodedSize()
{
return this.encodedSize;
}
}