2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* image_etc.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "image_etc.h"
|
|
|
|
#include "image.h"
|
|
|
|
#include "os/copymem.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "print_string.h"
|
|
|
|
#include "rg_etc1.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
static void _decompress_etc(Image *p_img) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND(p_img->get_format() != Image::FORMAT_ETC);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int imgw = p_img->get_width();
|
|
|
|
int imgh = p_img->get_height();
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<uint8_t> src = p_img->get_data();
|
2014-02-10 01:10:30 +00:00
|
|
|
DVector<uint8_t> dst;
|
|
|
|
|
|
|
|
DVector<uint8_t>::Read r = src.read();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int mmc = p_img->get_mipmaps();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i <= mmc; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
dst.resize(dst.size() + imgw * imgh * 3);
|
|
|
|
const uint8_t *srcbr = &r[p_img->get_mipmap_offset(i)];
|
2014-02-10 01:10:30 +00:00
|
|
|
DVector<uint8_t>::Write w = dst.write();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint8_t *wptr = &w[dst.size() - imgw * imgh * 3];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int bw = MAX(imgw / 4, 1);
|
|
|
|
int bh = MAX(imgh / 4, 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int y = 0; y < bh; y++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int x = 0; x < bw; x++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint8_t block[4 * 4 * 4];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
rg_etc1::unpack_etc1_block(srcbr, (unsigned int *)block);
|
|
|
|
srcbr += 8;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int maxx = MIN(imgw, 4);
|
|
|
|
int maxy = MIN(imgh, 4);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int yy = 0; yy < maxy; yy++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int xx = 0; xx < maxx; xx++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint32_t src_ofs = (yy * 4 + xx) * 4;
|
|
|
|
uint32_t dst_ofs = ((y * 4 + yy) * imgw + x * 4 + xx) * 3;
|
|
|
|
wptr[dst_ofs + 0] = block[src_ofs + 0];
|
|
|
|
wptr[dst_ofs + 1] = block[src_ofs + 1];
|
|
|
|
wptr[dst_ofs + 2] = block[src_ofs + 2];
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
imgw = MAX(1, imgw / 2);
|
|
|
|
imgh = MAX(1, imgh / 2);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
r = DVector<uint8_t>::Read();
|
2014-02-10 01:10:30 +00:00
|
|
|
//print_line("Re Creating ETC into regular image: w "+itos(p_img->get_width())+" h "+itos(p_img->get_height())+" mm "+itos(p_img->get_mipmaps()));
|
2017-03-18 23:36:26 +00:00
|
|
|
*p_img = Image(p_img->get_width(), p_img->get_height(), p_img->get_mipmaps(), Image::FORMAT_RGB, dst);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (p_img->get_mipmaps())
|
2017-03-18 23:36:26 +00:00
|
|
|
p_img->generate_mipmaps(-1, true);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void _compress_etc(Image *p_img) {
|
|
|
|
|
|
|
|
Image img = *p_img;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int imgw = img.get_width(), imgh = img.get_height();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-18 15:10:21 +00:00
|
|
|
ERR_FAIL_COND(next_power_of_2(imgw) != imgw || next_power_of_2(imgh) != imgh);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (img.get_format() != Image::FORMAT_RGB)
|
2014-02-10 01:10:30 +00:00
|
|
|
img.convert(Image::FORMAT_RGB);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int mmc = img.get_mipmaps();
|
|
|
|
if (mmc == 0)
|
2014-02-10 01:10:30 +00:00
|
|
|
img.generate_mipmaps(); // force mipmaps, so it works on most hardware
|
|
|
|
|
|
|
|
DVector<uint8_t> res_data;
|
|
|
|
DVector<uint8_t> dst_data;
|
|
|
|
DVector<uint8_t>::Read r = img.get_data().read();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int mc = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
rg_etc1::etc1_pack_params pp;
|
2017-03-18 23:36:26 +00:00
|
|
|
pp.m_quality = rg_etc1::cLowQuality;
|
|
|
|
for (int i = 0; i <= mmc; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int bw = MAX(imgw / 4, 1);
|
|
|
|
int bh = MAX(imgh / 4, 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
const uint8_t *src = &r[img.get_mipmap_offset(i)];
|
2017-03-18 23:36:26 +00:00
|
|
|
int mmsize = MAX(bw, 1) * MAX(bh, 1) * 8;
|
|
|
|
dst_data.resize(dst_data.size() + mmsize);
|
|
|
|
DVector<uint8_t>::Write w = dst_data.write();
|
|
|
|
uint8_t *dst = &w[dst_data.size() - mmsize];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
// print_line("bh: "+itos(bh)+" bw: "+itos(bw));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int y = 0; y < bh; y++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int x = 0; x < bw; x++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
// print_line("x: "+itos(x)+" y: "+itos(y));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint8_t block[4 * 4 * 4];
|
|
|
|
zeromem(block, 4 * 4 * 4);
|
2014-02-10 01:10:30 +00:00
|
|
|
uint8_t cblock[8];
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int maxy = MIN(imgh, 4);
|
|
|
|
int maxx = MIN(imgw, 4);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int yy = 0; yy < maxy; yy++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int xx = 0; xx < maxx; xx++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint32_t dst_ofs = (yy * 4 + xx) * 4;
|
|
|
|
uint32_t src_ofs = ((y * 4 + yy) * imgw + x * 4 + xx) * 3;
|
|
|
|
block[dst_ofs + 0] = src[src_ofs + 0];
|
|
|
|
block[dst_ofs + 1] = src[src_ofs + 1];
|
|
|
|
block[dst_ofs + 2] = src[src_ofs + 2];
|
|
|
|
block[dst_ofs + 3] = 255;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
rg_etc1::pack_etc1_block(cblock, (const unsigned int *)block, pp);
|
|
|
|
for (int j = 0; j < 8; j++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
dst[j] = cblock[j];
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
dst += 8;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
imgw = MAX(1, imgw / 2);
|
|
|
|
imgh = MAX(1, imgh / 2);
|
2014-02-10 01:10:30 +00:00
|
|
|
mc++;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
*p_img = Image(p_img->get_width(), p_img->get_height(), mc - 1, Image::FORMAT_ETC, dst_data);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _register_etc1_compress_func() {
|
|
|
|
|
|
|
|
rg_etc1::pack_etc1_block_init();
|
2017-03-18 23:36:26 +00:00
|
|
|
Image::_image_compress_etc_func = _compress_etc;
|
|
|
|
Image::_image_decompress_etc = _decompress_etc;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|