diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2016-10-11 23:35:58 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2016-10-15 11:50:39 +0200 |
commit | ee3cf211c6fd4d1e30617467cdbbe945798a68b3 (patch) | |
tree | d770150c48c806df4daca66770cde8d5b665a3ff /modules | |
parent | b1e8889d969f5f88539c47c2afac6c9ea2a2dc11 (diff) |
webp: Make it a module and unbundle libwebp thirdparty files
Note that there are two Godot-specific changes made to libwebp
for the javascript/HTML5 platform. They are documented in the
README.md.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/webp/SCsub | 120 | ||||
-rw-r--r-- | modules/webp/config.py | 6 | ||||
-rw-r--r-- | modules/webp/image_loader_webp.cpp | 183 | ||||
-rw-r--r-- | modules/webp/image_loader_webp.h | 49 | ||||
-rw-r--r-- | modules/webp/register_types.cpp | 44 | ||||
-rw-r--r-- | modules/webp/register_types.h | 30 |
6 files changed, 432 insertions, 0 deletions
diff --git a/modules/webp/SCsub b/modules/webp/SCsub new file mode 100644 index 0000000000..f52c5b9af8 --- /dev/null +++ b/modules/webp/SCsub @@ -0,0 +1,120 @@ +Import('env') +Import('env_modules') + +# Thirdparty source files +if (env["libwebp"] != "system"): # builtin + thirdparty_dir = "#thirdparty/libwebp/" + thirdparty_libwebp_sources = [ + "enc/webpenc.c", + "enc/near_lossless.c", + "enc/frame.c", + "enc/alpha.c", + "enc/picture_csp.c", + "enc/vp8l.c", + "enc/picture_psnr.c", + "enc/delta_palettization.c", + "enc/syntax.c", + "enc/backward_references.c", + "enc/token.c", + "enc/analysis.c", + "enc/iterator.c", + "enc/picture_tools.c", + "enc/picture_rescale.c", + "enc/config.c", + "enc/tree.c", + "enc/cost.c", + "enc/picture.c", + "enc/quant.c", + "enc/filter.c", + "enc/histogram.c", + "utils/rescaler.c", + "utils/filters.c", + "utils/quant_levels_dec.c", + "utils/huffman.c", + "utils/thread.c", + "utils/quant_levels.c", + "utils/bit_writer.c", + "utils/bit_reader.c", + "utils/random.c", + "utils/utils.c", + "utils/huffman_encode.c", + "utils/color_cache.c", + "mux/muxinternal.c", + "mux/muxread.c", + "mux/anim_encode.c", + "mux/muxedit.c", + "dec/webp.c", + "dec/frame.c", + "dec/alpha.c", + "dec/vp8l.c", + "dec/io.c", + "dec/vp8.c", + "dec/idec.c", + "dec/tree.c", + "dec/buffer.c", + "dec/quant.c", + "demux/demux.c", + "demux/anim_decode.c", + "dsp/yuv.c", + "dsp/filters_sse2.c", + "dsp/dec_sse41.c", + "dsp/rescaler.c", + "dsp/lossless_sse2.c", + "dsp/alpha_processing_sse41.c", + "dsp/alpha_processing_sse2.c", + "dsp/filters.c", + "dsp/upsampling_mips_dsp_r2.c", + "dsp/dec_neon.c", + "dsp/enc_neon.c", + "dsp/lossless_enc_mips32.c", + "dsp/lossless_enc_sse2.c", + "dsp/upsampling.c", + "dsp/lossless_enc_neon.c", + "dsp/alpha_processing.c", + "dsp/cost_sse2.c", + "dsp/dec_mips32.c", + "dsp/enc_avx2.c", + "dsp/rescaler_mips32.c", + "dsp/enc.c", + "dsp/lossless_enc_sse41.c", + "dsp/cost_mips32.c", + "dsp/lossless_mips_dsp_r2.c", + "dsp/filters_mips_dsp_r2.c", + "dsp/upsampling_neon.c", + "dsp/alpha_processing_mips_dsp_r2.c", + "dsp/enc_mips_dsp_r2.c", + "dsp/lossless.c", + "dsp/yuv_mips_dsp_r2.c", + "dsp/cost_mips_dsp_r2.c", + "dsp/argb.c", + "dsp/dec_sse2.c", + "dsp/rescaler_sse2.c", + "dsp/enc_sse41.c", + "dsp/argb_mips_dsp_r2.c", + "dsp/lossless_enc_mips_dsp_r2.c", + "dsp/dec_clip_tables.c", + "dsp/yuv_mips32.c", + "dsp/cpu.c", + "dsp/dec.c", + "dsp/argb_sse2.c", + "dsp/lossless_neon.c", + "dsp/lossless_enc.c", + "dsp/enc_mips32.c", + "dsp/cost.c", + "dsp/rescaler_mips_dsp_r2.c", + "dsp/dec_mips_dsp_r2.c", + "dsp/rescaler_neon.c", + "dsp/yuv_sse2.c", + "dsp/enc_sse2.c", + "dsp/upsampling_sse2.c", + ] + thirdparty_libwebp_sources = [thirdparty_dir + file for file in thirdparty_libwebp_sources] + + env_modules.add_source_files(env.modules_sources, thirdparty_libwebp_sources) + env_modules.Append(CPPPATH = [thirdparty_dir]) + +# Godot source files +env_modules.add_source_files(env.modules_sources, "*.cpp") + +Export('env_modules') +Export('env') diff --git a/modules/webp/config.py b/modules/webp/config.py new file mode 100644 index 0000000000..368e97e152 --- /dev/null +++ b/modules/webp/config.py @@ -0,0 +1,6 @@ + +def can_build(platform): + return True + +def configure(env): + pass diff --git a/modules/webp/image_loader_webp.cpp b/modules/webp/image_loader_webp.cpp new file mode 100644 index 0000000000..0fe2db3261 --- /dev/null +++ b/modules/webp/image_loader_webp.cpp @@ -0,0 +1,183 @@ +/*************************************************************************/ +/* image_loader_webp.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/*************************************************************************/ +#include "image_loader_webp.h" + +#include "io/marshalls.h" +#include "print_string.h" +#include "os/os.h" + +#include <stdlib.h> +#include <webp/decode.h> +#include <webp/encode.h> + +static DVector<uint8_t> _webp_lossy_pack(const Image& p_image,float p_quality) { + + ERR_FAIL_COND_V(p_image.empty(),DVector<uint8_t>()); + + Image img=p_image; + if (img.detect_alpha()) + img.convert(Image::FORMAT_RGBA); + else + img.convert(Image::FORMAT_RGB); + + Size2 s(img.get_width(),img.get_height()); + DVector<uint8_t> data = img.get_data(); + DVector<uint8_t>::Read r = data.read(); + + uint8_t *dst_buff=NULL; + size_t dst_size=0; + if (img.get_format()==Image::FORMAT_RGB) { + + dst_size = WebPEncodeRGB(r.ptr(),s.width,s.height,3*s.width,CLAMP(p_quality*100.0,0,100.0),&dst_buff); + } else { + dst_size = WebPEncodeRGBA(r.ptr(),s.width,s.height,4*s.width,CLAMP(p_quality*100.0,0,100.0),&dst_buff); + } + + ERR_FAIL_COND_V(dst_size==0,DVector<uint8_t>()); + DVector<uint8_t> dst; + dst.resize(4+dst_size); + DVector<uint8_t>::Write w = dst.write(); + w[0]='W'; + w[1]='E'; + w[2]='B'; + w[3]='P'; + copymem(&w[4],dst_buff,dst_size); + free(dst_buff); + w=DVector<uint8_t>::Write(); + return dst; +} + +static Image _webp_lossy_unpack(const DVector<uint8_t>& p_buffer) { + + int size = p_buffer.size()-4; + ERR_FAIL_COND_V(size<=0,Image()); + DVector<uint8_t>::Read r = p_buffer.read(); + + ERR_FAIL_COND_V(r[0]!='W' || r[1]!='E' || r[2]!='B' || r[3]!='P',Image()); + WebPBitstreamFeatures features; + if (WebPGetFeatures(&r[4],size,&features)!=VP8_STATUS_OK) { + ERR_EXPLAIN("Error unpacking WEBP image:"); + ERR_FAIL_V(Image()); + } + + //print_line("width: "+itos(features.width)); + //print_line("height: "+itos(features.height)); + //print_line("alpha: "+itos(features.has_alpha)); + + DVector<uint8_t> dst_image; + int datasize = features.width*features.height*(features.has_alpha?4:3); + dst_image.resize(datasize); + + DVector<uint8_t>::Write dst_w = dst_image.write(); + + bool errdec=false; + if (features.has_alpha) { + errdec = WebPDecodeRGBAInto(&r[4],size,dst_w.ptr(),datasize,4*features.width)==NULL; + } else { + errdec = WebPDecodeRGBInto(&r[4],size,dst_w.ptr(),datasize,3*features.width)==NULL; + + } + + //ERR_EXPLAIN("Error decoding webp! - "+p_file); + ERR_FAIL_COND_V(errdec,Image()); + + dst_w = DVector<uint8_t>::Write(); + + return Image(features.width,features.height,0,features.has_alpha?Image::FORMAT_RGBA:Image::FORMAT_RGB,dst_image); + +} + + +Error ImageLoaderWEBP::load_image(Image *p_image,FileAccess *f) { + + + uint32_t size = f->get_len(); + DVector<uint8_t> src_image; + src_image.resize(size); + + WebPBitstreamFeatures features; + + DVector<uint8_t>::Write src_w = src_image.write(); + f->get_buffer(src_w.ptr(),size); + ERR_FAIL_COND_V(f->eof_reached(), ERR_FILE_EOF); + + if (WebPGetFeatures(src_w.ptr(),size,&features)!=VP8_STATUS_OK) { + f->close(); + //ERR_EXPLAIN("Error decoding WEBP image: "+p_file); + ERR_FAIL_V(ERR_FILE_CORRUPT); + } + + print_line("width: "+itos(features.width)); + print_line("height: "+itos(features.height)); + print_line("alpha: "+itos(features.has_alpha)); + + src_w = DVector<uint8_t>::Write(); + + DVector<uint8_t> dst_image; + int datasize = features.width*features.height*(features.has_alpha?4:3); + dst_image.resize(datasize); + + DVector<uint8_t>::Read src_r = src_image.read(); + DVector<uint8_t>::Write dst_w = dst_image.write(); + + + bool errdec=false; + if (features.has_alpha) { + errdec = WebPDecodeRGBAInto(src_r.ptr(),size,dst_w.ptr(),datasize,4*features.width)==NULL; + } else { + errdec = WebPDecodeRGBInto(src_r.ptr(),size,dst_w.ptr(),datasize,3*features.width)==NULL; + + } + + //ERR_EXPLAIN("Error decoding webp! - "+p_file); + ERR_FAIL_COND_V(errdec,ERR_FILE_CORRUPT); + + src_r = DVector<uint8_t>::Read(); + dst_w = DVector<uint8_t>::Write(); + + *p_image = Image(features.width,features.height,0,features.has_alpha?Image::FORMAT_RGBA:Image::FORMAT_RGB,dst_image); + + + return OK; + +} + +void ImageLoaderWEBP::get_recognized_extensions(List<String> *p_extensions) const { + + p_extensions->push_back("webp"); +} + + +ImageLoaderWEBP::ImageLoaderWEBP() { + + Image::lossy_packer=_webp_lossy_pack; + Image::lossy_unpacker=_webp_lossy_unpack; +} + + diff --git a/modules/webp/image_loader_webp.h b/modules/webp/image_loader_webp.h new file mode 100644 index 0000000000..24f79708db --- /dev/null +++ b/modules/webp/image_loader_webp.h @@ -0,0 +1,49 @@ +/*************************************************************************/ +/* image_loader_webp.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/*************************************************************************/ +#ifndef IMAGE_LOADER_WEBP_H +#define IMAGE_LOADER_WEBP_H + +#include "io/image_loader.h" + +/** + @author Juan Linietsky <reduzio@gmail.com> +*/ +class ImageLoaderWEBP : public ImageFormatLoader { + + +public: + + virtual Error load_image(Image *p_image,FileAccess *f); + virtual void get_recognized_extensions(List<String> *p_extensions) const; + ImageLoaderWEBP(); +}; + + + +#endif diff --git a/modules/webp/register_types.cpp b/modules/webp/register_types.cpp new file mode 100644 index 0000000000..039876bbb9 --- /dev/null +++ b/modules/webp/register_types.cpp @@ -0,0 +1,44 @@ +/*************************************************************************/ +/* register_types.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/*************************************************************************/ +#include "register_types.h" + +#include "image_loader_webp.h" + +static ImageLoaderWEBP *image_loader_webp = NULL; + +void register_webp_types() { + + image_loader_webp = memnew( ImageLoaderWEBP ); + ImageLoader::add_image_format_loader(image_loader_webp); +} + +void unregister_webp_types() { + + memdelete( image_loader_webp ); +} diff --git a/modules/webp/register_types.h b/modules/webp/register_types.h new file mode 100644 index 0000000000..a200188e47 --- /dev/null +++ b/modules/webp/register_types.h @@ -0,0 +1,30 @@ +/*************************************************************************/ +/* register_types.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/*************************************************************************/ +void register_webp_types(); +void unregister_webp_types(); |