18cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Copyright 2009 Google Inc.
28cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//
38cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Licensed under the Apache License, Version 2.0 (the "License");
48cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// you may not use this file except in compliance with the License.
58cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// You may obtain a copy of the License at
68cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//
78cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//     http://www.apache.org/licenses/LICENSE-2.0
88cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//
98cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Unless required by applicable law or agreed to in writing, software
108cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// distributed under the License is distributed on an "AS IS" BASIS,
118cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// See the License for the specific language governing permissions and
138cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// limitations under the License.
148cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
158cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//////////////////////////////////////////////////////////////////////////////////////////
168cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
178cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// This is a fork of the AOSP project ETC1 codec. The original code can be found
188cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// at the following web site:
198cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// https://android.googlesource.com/platform/frameworks/native/+/master/opengl/libs/ETC1/
208cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
218cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//////////////////////////////////////////////////////////////////////////////////////////
228cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
238cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#ifndef __etc1_h__
248cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#define __etc1_h__
258cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
268cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#define ETC1_ENCODED_BLOCK_SIZE 8
278cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#define ETC1_DECODED_BLOCK_SIZE 48
288cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
298cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#ifndef ETC1_RGB8_OES
308cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#define ETC1_RGB8_OES 0x8D64
318cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#endif
328cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
338cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.comtypedef unsigned char etc1_byte;
348cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.comtypedef int etc1_bool;
358cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.comtypedef unsigned int etc1_uint32;
368cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
378cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#ifdef __cplusplus
388cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.comextern "C" {
398cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#endif
408cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
418cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Encode a block of pixels.
428cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//
438cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// pIn is a pointer to a ETC_DECODED_BLOCK_SIZE array of bytes that represent a
448cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// 4 x 4 square of 3-byte pixels in form R, G, B. Byte (3 * (x + 4 * y) is the R
458cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// value of pixel (x, y).
468cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//
478cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// validPixelMask is a 16-bit mask where bit (1 << (x + y * 4)) indicates whether
488cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// the corresponding (x,y) pixel is valid. Invalid pixel color values are ignored when compressing.
498cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//
508cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// pOut is an ETC1 compressed version of the data.
518cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
528cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.comvoid etc1_encode_block(const etc1_byte* pIn, etc1_uint32 validPixelMask, etc1_byte* pOut);
538cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
548cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Decode a block of pixels.
558cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//
568cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// pIn is an ETC1 compressed version of the data.
578cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//
588cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// pOut is a pointer to a ETC_DECODED_BLOCK_SIZE array of bytes that represent a
598cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// 4 x 4 square of 3-byte pixels in form R, G, B. Byte (3 * (x + 4 * y) is the R
608cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// value of pixel (x, y).
618cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
628cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.comvoid etc1_decode_block(const etc1_byte* pIn, etc1_byte* pOut);
638cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
648cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Return the size of the encoded image data (does not include size of PKM header).
658cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
668cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.cometc1_uint32 etc1_get_encoded_data_size(etc1_uint32 width, etc1_uint32 height);
678cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
688cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Encode an entire image.
698cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// pIn - pointer to the image data. Formatted such that
708cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//       pixel (x,y) is at pIn + pixelSize * x + stride * y;
718cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// pOut - pointer to encoded data. Must be large enough to store entire encoded image.
728cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// pixelSize can be 2 or 3. 2 is an GL_UNSIGNED_SHORT_5_6_5 image, 3 is a GL_BYTE RGB image.
738cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// returns non-zero if there is an error.
748cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
758cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.comint etc1_encode_image(const etc1_byte* pIn, etc1_uint32 width, etc1_uint32 height,
768cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com        etc1_uint32 pixelSize, etc1_uint32 stride, etc1_byte* pOut);
778cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
788cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Decode an entire image.
798cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// pIn - pointer to encoded data.
808cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// pOut - pointer to the image data. Will be written such that
818cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//        pixel (x,y) is at pIn + pixelSize * x + stride * y. Must be
828cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com//        large enough to store entire image.
838cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// pixelSize can be 2 or 3. 2 is an GL_UNSIGNED_SHORT_5_6_5 image, 3 is a GL_BYTE RGB image.
848cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// returns non-zero if there is an error.
858cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
868cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.comint etc1_decode_image(const etc1_byte* pIn, etc1_byte* pOut,
878cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com        etc1_uint32 width, etc1_uint32 height,
888cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com        etc1_uint32 pixelSize, etc1_uint32 stride);
898cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
908cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Size of a PKM header, in bytes.
918cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
928cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#define ETC_PKM_HEADER_SIZE 16
938cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
948cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Format a PKM header
958cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
968cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.comvoid etc1_pkm_format_header(etc1_byte* pHeader, etc1_uint32 width, etc1_uint32 height);
978cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
988cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Check if a PKM header is correctly formatted.
998cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
1008cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.cometc1_bool etc1_pkm_is_valid(const etc1_byte* pHeader);
1018cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
1028cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Read the image width from a PKM header
1038cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
1048cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.cometc1_uint32 etc1_pkm_get_width(const etc1_byte* pHeader);
1058cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
1068cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com// Read the image height from a PKM header
1078cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
1088cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.cometc1_uint32 etc1_pkm_get_height(const etc1_byte* pHeader);
1098cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
1108cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#ifdef __cplusplus
1118cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com}
1128cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#endif
1138cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com
1148cf81e0f4fa2a8054ac4cea1e7490028809cb893robertphillips@google.com#endif
115