1a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Copyright 2009 Google Inc.
2a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//
3a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Licensed under the Apache License, Version 2.0 (the "License");
4a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// you may not use this file except in compliance with the License.
5a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// You may obtain a copy of the License at
6a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//
7a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//     http://www.apache.org/licenses/LICENSE-2.0
8a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//
9a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Unless required by applicable law or agreed to in writing, software
10a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// distributed under the License is distributed on an "AS IS" BASIS,
11a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// See the License for the specific language governing permissions and
13a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// limitations under the License.
14a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
15a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#ifndef __etc1_h__
16a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#define __etc1_h__
17a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
18a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#define ETC1_ENCODED_BLOCK_SIZE 8
19a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#define ETC1_DECODED_BLOCK_SIZE 48
20a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
21a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#ifndef ETC1_RGB8_OES
22a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#define ETC1_RGB8_OES 0x8D64
23a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#endif
24a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
25a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichtypedef unsigned char etc1_byte;
26a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichtypedef int etc1_bool;
27a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichtypedef unsigned int etc1_uint32;
28a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
29a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#ifdef __cplusplus
30a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichextern "C" {
31a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#endif
32a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
33a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Encode a block of pixels.
34a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//
35a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// pIn is a pointer to a ETC_DECODED_BLOCK_SIZE array of bytes that represent a
36a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// 4 x 4 square of 3-byte pixels in form R, G, B. Byte (3 * (x + 4 * y) is the R
37a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// value of pixel (x, y).
38a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//
39a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// validPixelMask is a 16-bit mask where bit (1 << (x + y * 4)) indicates whether
40a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// the corresponding (x,y) pixel is valid. Invalid pixel color values are ignored when compressing.
41a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//
42a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// pOut is an ETC1 compressed version of the data.
43a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
44a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichvoid etc1_encode_block(const etc1_byte* pIn, etc1_uint32 validPixelMask, etc1_byte* pOut);
45a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
46a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Decode a block of pixels.
47a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//
48a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// pIn is an ETC1 compressed version of the data.
49a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//
50a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// pOut is a pointer to a ETC_DECODED_BLOCK_SIZE array of bytes that represent a
51a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// 4 x 4 square of 3-byte pixels in form R, G, B. Byte (3 * (x + 4 * y) is the R
52a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// value of pixel (x, y).
53a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
54a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichvoid etc1_decode_block(const etc1_byte* pIn, etc1_byte* pOut);
55a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
56a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Return the size of the encoded image data (does not include size of PKM header).
57a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
58a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichetc1_uint32 etc1_get_encoded_data_size(etc1_uint32 width, etc1_uint32 height);
59a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
60a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Encode an entire image.
61a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// pIn - pointer to the image data. Formatted such that
62a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//       pixel (x,y) is at pIn + pixelSize * x + stride * y;
63a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// pOut - pointer to encoded data. Must be large enough to store entire encoded image.
64a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// pixelSize can be 2 or 3. 2 is an GL_UNSIGNED_SHORT_5_6_5 image, 3 is a GL_BYTE RGB image.
65a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// returns non-zero if there is an error.
66a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
67a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichint etc1_encode_image(const etc1_byte* pIn, etc1_uint32 width, etc1_uint32 height,
68a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich        etc1_uint32 pixelSize, etc1_uint32 stride, etc1_byte* pOut);
69a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
70a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Decode an entire image.
71a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// pIn - pointer to encoded data.
72a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// pOut - pointer to the image data. Will be written such that
73a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//        pixel (x,y) is at pIn + pixelSize * x + stride * y. Must be
74a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich//        large enough to store entire image.
75a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// pixelSize can be 2 or 3. 2 is an GL_UNSIGNED_SHORT_5_6_5 image, 3 is a GL_BYTE RGB image.
76a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// returns non-zero if there is an error.
77a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
78a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichint etc1_decode_image(const etc1_byte* pIn, etc1_byte* pOut,
79a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich        etc1_uint32 width, etc1_uint32 height,
80a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich        etc1_uint32 pixelSize, etc1_uint32 stride);
81a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
82a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Size of a PKM header, in bytes.
83a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
84a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#define ETC_PKM_HEADER_SIZE 16
85a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
86a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Format a PKM header
87a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
88a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichvoid etc1_pkm_format_header(etc1_byte* pHeader, etc1_uint32 width, etc1_uint32 height);
89a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
90a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Check if a PKM header is correctly formatted.
91a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
92a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichetc1_bool etc1_pkm_is_valid(const etc1_byte* pHeader);
93a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
94a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Read the image width from a PKM header
95a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
96a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichetc1_uint32 etc1_pkm_get_width(const etc1_byte* pHeader);
97a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
98a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich// Read the image height from a PKM header
99a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
100a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevichetc1_uint32 etc1_pkm_get_height(const etc1_byte* pHeader);
101a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
102a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#ifdef __cplusplus
103a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich}
104a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#endif
105a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich
106a6276fdd4253c3a7150ab675678c750473ab6c45Jack Palevich#endif
107