1/*
2 * Copyright (C) 2009 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
17package android.opengl;
18
19import java.nio.Buffer;
20
21/**
22 * Methods for encoding and decoding ETC1 textures.
23 * <p>
24 * The standard for the ETC1 texture format can be found at
25 * http://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8_texture.txt
26 * <p>
27 * The PKM file format is of a 16-byte header that describes the image bounds
28 * followed by the encoded ETC1 texture data.
29 * <p>
30 * @see ETC1Util
31 */
32public class ETC1 {
33
34    /**
35     * Size in bytes of an encoded block.
36     */
37    public static final int ENCODED_BLOCK_SIZE = 8;
38
39    /**
40     * Size in bytes of a decoded block.
41     */
42    public static final int DECODED_BLOCK_SIZE = 48;
43
44    /**
45     * Size of a PKM file header, in bytes.
46     */
47    public static final int ETC_PKM_HEADER_SIZE = 16;
48
49    /**
50     * Accepted by the internalformat parameter of glCompressedTexImage2D.
51     */
52    public static final int ETC1_RGB8_OES = 0x8D64;
53
54    /**
55     * Encode a block of pixels.
56     *
57     * @param in a native order direct buffer of size DECODED_BLOCK_SIZE that represent a
58     * 4 x 4 square of 3-byte pixels in form R, G, B. Byte (3 * (x + 4 * y) is the R
59     * value of pixel (x, y).
60     *
61     * @param validPixelMask is a 16-bit mask where bit (1 << (x + y * 4)) indicates whether
62     * the corresponding (x,y) pixel is valid. Invalid pixel color values are ignored when compressing.
63     *
64     * @param out a native order direct buffer of size ENCODED_BLOCK_SIZE that receives the
65     * ETC1 compressed version of the data.
66     *
67     */
68    public static native void encodeBlock(Buffer in, int validPixelMask, Buffer out);
69
70    /**
71     * Decode a block of pixels.
72     *
73     * @param in a native order direct buffer of size ENCODED_BLOCK_SIZE that contains the
74     * ETC1 compressed version of the data.
75     *
76     * @param out a native order direct buffer of size DECODED_BLOCK_SIZE that will receive
77     * the decoded data. The data represents a
78     * 4 x 4 square of 3-byte pixels in form R, G, B. Byte (3 * (x + 4 * y) is the R
79     * value of pixel (x, y).
80     */
81    public static native void decodeBlock(Buffer in, Buffer out);
82
83    /**
84     * Return the size of the encoded image data (does not include size of PKM header).
85     */
86    public static native int getEncodedDataSize(int width, int height);
87
88    /**
89     * Encode an entire image.
90     * @param in a native order direct buffer of the image data. Formatted such that
91     *           pixel (x,y) is at pIn + pixelSize * x + stride * y;
92     * @param out a native order direct buffer of the encoded data.
93     * Must be large enough to store entire encoded image.
94     * @param pixelSize must be 2 or 3. 2 is an GL_UNSIGNED_SHORT_5_6_5 image,
95     * 3 is a GL_BYTE RGB image.
96     */
97    public static native void encodeImage(Buffer in, int width, int height,
98            int pixelSize, int stride, Buffer out);
99
100    /**
101     * Decode an entire image.
102     * @param in native order direct buffer of the encoded data.
103     * @param out native order direct buffer of the image data. Will be written such that
104     * pixel (x,y) is at pIn + pixelSize * x + stride * y. Must be
105     * large enough to store entire image.
106     * @param pixelSize must be 2 or 3. 2 is an GL_UNSIGNED_SHORT_5_6_5 image,
107     * 3 is a GL_BYTE RGB image.
108     */
109    public static native void decodeImage(Buffer in, Buffer out,
110            int width, int height, int pixelSize, int stride);
111
112    /**
113     * Format a PKM header
114     * @param header native order direct buffer of the header.
115     * @param width the width of the image in pixels.
116     * @param height the height of the image in pixels.
117     */
118    public static native void formatHeader(Buffer header, int width, int height);
119
120    /**
121     * Check if a PKM header is correctly formatted.
122     * @param header native order direct buffer of the header.
123     */
124    public static native boolean isValid(Buffer header);
125
126    /**
127     * Read the image width from a PKM header
128     * @param header native order direct buffer of the header.
129     */
130    public static native int getWidth(Buffer header);
131
132    /**
133     * Read the image height from a PKM header
134     * @param header native order direct buffer of the header.
135     */
136    public static native int getHeight(Buffer header);
137}
138