]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_rg_etc1.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_rg_etc1.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: rg_etc1.h - Fast, high quality ETC1 block packer/unpacker - Rich Geldreich <richgel99@gmail.com>
28 // Please see ZLIB license at the end of this file.
29 #pragma once
30
31 #include "vogl_core.h"
32
33 namespace vogl
34 {
35
36     namespace rg_etc1
37     {
38         // Unpacks an 8-byte ETC1 compressed block to a block of 4x4 32bpp RGBA pixels.
39         // Returns false if the block is invalid. Invalid blocks will still be unpacked with clamping.
40         // This function is thread safe, and does not dynamically allocate any memory.
41         // If preserve_alpha is true, the alpha channel of the destination pixels will not be overwritten. Otherwise, alpha will be set to 255.
42         bool unpack_etc1_block(const void *pETC1_block, unsigned int *pDst_pixels_rgba, bool preserve_alpha = false);
43
44         // Quality setting = the higher the quality, the slower.
45         // To pack large textures, it is highly recommended to call pack_etc1_block() in parallel, on different blocks, from multiple threads (particularly when using cHighQuality).
46         enum etc1_quality
47         {
48             cLowQuality,
49             cMediumQuality,
50             cHighQuality,
51         };
52
53         struct etc1_pack_params
54         {
55             etc1_quality m_quality;
56             bool m_dithering;
57
58             inline etc1_pack_params()
59             {
60                 clear();
61             }
62
63             void clear()
64             {
65                 m_quality = cHighQuality;
66                 m_dithering = false;
67             }
68         };
69
70         // Important: pack_etc1_block_init() must be called before calling pack_etc1_block().
71         void pack_etc1_block_init();
72
73         // Packs a 4x4 block of 32bpp RGBA pixels to an 8-byte ETC1 block.
74         // 32-bit RGBA pixels must always be arranged as (R,G,B,A) (R first, A last) in memory, independent of platform endianness. A should always be 255.
75         // Returns squared error of result.
76         // This function is thread safe, and does not dynamically allocate any memory.
77         // pack_etc1_block() does not currently support "perceptual" colorspace metrics - it primarily optimizes for RGB RMSE.
78         unsigned int pack_etc1_block(void *pETC1_block, const unsigned int *pSrc_pixels_rgba, etc1_pack_params &pack_params);
79
80     } // namespace rg_etc1
81
82 } // namespace vogl
83
84 //------------------------------------------------------------------------------
85 //
86 // rg_etc1 uses the ZLIB license:
87 // http://opensource.org/licenses/Zlib
88 //
89 // Copyright (c) 2012 Rich Geldreich
90 //
91 // This software is provided 'as-is', without any express or implied
92 // warranty.  In no event will the authors be held liable for any damages
93 // arising from the use of this software.
94 //
95 // Permission is granted to anyone to use this software for any purpose,
96 // including commercial applications, and to alter it and redistribute it
97 // freely, subject to the following restrictions:
98 //
99 // 1. The origin of this software must not be misrepresented; you must not
100 // claim that you wrote the original software. If you use this software
101 // in a product, an acknowledgment in the product documentation would be
102 // appreciated but is not required.
103 //
104 // 2. Altered source versions must be plainly marked as such, and must not be
105 // misrepresented as being the original software.
106 //
107 // 3. This notice may not be removed or altered from any source distribution.
108 //
109 //------------------------------------------------------------------------------