├── .gitignore ├── FlashRuntimeExtensions.h ├── FlashRuntimeExtensions.lib ├── README.md ├── ane ├── .gitignore ├── android │ ├── .gitignore │ ├── libAndroid.so │ └── library.swf ├── as3 │ └── libwebp │ │ ├── DecodeWebp.as │ │ ├── EncodeWebp.as │ │ └── Webp.as ├── build_android.bat ├── build_android.sh ├── default.properties ├── default │ ├── .gitignore │ └── library.swf ├── extension.xml ├── ios │ └── .gitignore ├── jni │ ├── Android.mk │ └── external │ │ ├── include │ │ └── FlashRuntimeExtensions.h │ │ └── lib │ │ └── FlashRuntimeExtensions.so ├── libs │ ├── armeabi │ │ └── FlashRuntimeExtensions.so │ └── win │ │ └── FlashRuntimeExtensions.h ├── mac │ ├── libMacOS.framework │ │ └── libMacOS │ └── library.swf ├── test.p12 └── windows │ ├── .gitignore │ ├── libWindows.dll │ └── library.swf ├── as3api.c ├── as3api_encode.c ├── as3libwebp.iml ├── as3libwebp.ipr ├── bin ├── .gitignore ├── libwebp.ane ├── libwebp.swc └── libwebp_decode.swc ├── buildswc.php ├── docs ├── Adobe Flash Platform - Building the native library.url └── ane-lab - AIR Native Extension is Lab codes - Google Project Hosting.url ├── html-template ├── history │ ├── history.css │ ├── history.js │ └── historyFrame.html ├── index.template.html └── swfobject.js ├── libwebp └── src │ ├── Makefile.am │ ├── dec │ ├── Makefile.am │ ├── alpha.c │ ├── alphai.h │ ├── buffer.c │ ├── decode_vp8.h │ ├── frame.c │ ├── idec.c │ ├── io.c │ ├── quant.c │ ├── tree.c │ ├── vp8.c │ ├── vp8i.h │ ├── vp8l.c │ ├── vp8li.h │ ├── webp.c │ └── webpi.h │ ├── demux │ ├── Makefile.am │ ├── demux.c │ └── libwebpdemux.pc.in │ ├── dsp │ ├── Makefile.am │ ├── alpha_processing.c │ ├── alpha_processing_sse2.c │ ├── cpu.c │ ├── dec.c │ ├── dec_clip_tables.c │ ├── dec_mips32.c │ ├── dec_neon.c │ ├── dec_sse2.c │ ├── dsp.h │ ├── enc.c │ ├── enc_avx2.c │ ├── enc_mips32.c │ ├── enc_neon.c │ ├── enc_sse2.c │ ├── lossless.c │ ├── lossless.h │ ├── lossless_mips32.c │ ├── lossless_neon.c │ ├── lossless_sse2.c │ ├── neon.h │ ├── upsampling.c │ ├── upsampling_neon.c │ ├── upsampling_sse2.c │ ├── yuv.c │ ├── yuv.h │ ├── yuv_mips32.c │ ├── yuv_sse2.c │ └── yuv_tables_sse2.h │ ├── enc │ ├── Makefile.am │ ├── alpha.c │ ├── analysis.c │ ├── backward_references.c │ ├── backward_references.h │ ├── config.c │ ├── cost.c │ ├── cost.h │ ├── filter.c │ ├── frame.c │ ├── histogram.c │ ├── histogram.h │ ├── iterator.c │ ├── picture.c │ ├── picture_csp.c │ ├── picture_psnr.c │ ├── picture_rescale.c │ ├── picture_tools.c │ ├── quant.c │ ├── syntax.c │ ├── token.c │ ├── tree.c │ ├── vp8enci.h │ ├── vp8l.c │ ├── vp8li.h │ └── webpenc.c │ ├── libwebp.pc.in │ ├── libwebpdecoder.pc.in │ ├── mux │ ├── Makefile.am │ ├── libwebpmux.pc.in │ ├── muxedit.c │ ├── muxi.h │ ├── muxinternal.c │ └── muxread.c │ ├── utils │ ├── Makefile.am │ ├── bit_reader.c │ ├── bit_reader.h │ ├── bit_reader_inl.h │ ├── bit_writer.c │ ├── bit_writer.h │ ├── color_cache.c │ ├── color_cache.h │ ├── endian_inl.h │ ├── filters.c │ ├── filters.h │ ├── huffman.c │ ├── huffman.h │ ├── huffman_encode.c │ ├── huffman_encode.h │ ├── quant_levels.c │ ├── quant_levels.h │ ├── quant_levels_dec.c │ ├── quant_levels_dec.h │ ├── random.c │ ├── random.h │ ├── rescaler.c │ ├── rescaler.h │ ├── thread.c │ ├── thread.h │ ├── utils.c │ └── utils.h │ └── webp │ ├── decode.h │ ├── demux.h │ ├── encode.h │ ├── format_constants.h │ ├── mux.h │ ├── mux_types.h │ └── types.h ├── sample ├── .gitignore ├── sample.iml ├── sample.ipr └── src │ ├── MainFileSelector.as │ ├── MouseWheel.as │ ├── sample.jpg │ ├── test.webp │ ├── test3.webp │ └── texture.webp └── webp_extension.c /.gitignore: -------------------------------------------------------------------------------- 1 | /ane/obj 2 | *.obj 3 | *.o 4 | -------------------------------------------------------------------------------- /FlashRuntimeExtensions.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/FlashRuntimeExtensions.lib -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | as3libwebp 2 | ========== 3 | 4 | Load and save WEBP images on Flash using FlashCC or faster ANEs 5 | 6 | ### Sample 7 | 8 | http://soywiz.github.io/as3libwebp/ 9 | 10 | [](http://soywiz.github.io/as3libwebp/) 11 | 12 | ### API 13 | 14 | ```as3 15 | package libwebp; 16 | function DecodeWebp(webpByteArray:ByteArray):BitmapData; 17 | function EncodeWebp(input:BitmapData, quality:Number):ByteArray; 18 | ``` 19 | 20 | SWC supported on all platforms including web. It uses FlashCC so it is portable but runs ~3x-5x times slower: 21 | * You must include bin/libwebp.swc 22 | 23 | ANE supported for AIR Windows and Android. It uses native libraries so it will run as fast as possible: 24 | * You must include bin/libwebp.ane 25 | 26 | -------------------------------------------------------------------------------- /ane/.gitignore: -------------------------------------------------------------------------------- 1 | *.exp 2 | *.lib 3 | *.ane 4 | *.swc 5 | *.zip 6 | -------------------------------------------------------------------------------- /ane/android/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ane/android/libAndroid.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/ane/android/libAndroid.so -------------------------------------------------------------------------------- /ane/android/library.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/ane/android/library.swf -------------------------------------------------------------------------------- /ane/as3/libwebp/DecodeWebp.as: -------------------------------------------------------------------------------- 1 | package libwebp 2 | { 3 | import flash.display.BitmapData; 4 | import flash.utils.ByteArray; 5 | 6 | public function DecodeWebp(data:ByteArray):BitmapData 7 | { 8 | return BitmapData(Webp.context.call('WebpDecodeAne', data)); 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /ane/as3/libwebp/EncodeWebp.as: -------------------------------------------------------------------------------- 1 | package libwebp 2 | { 3 | import flash.display.BitmapData; 4 | import flash.utils.ByteArray; 5 | 6 | public function EncodeWebp(input:BitmapData, quality:Number):ByteArray 7 | { 8 | return ByteArray(Webp.context.call('WebpEncodeAne', input, quality)); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /ane/as3/libwebp/Webp.as: -------------------------------------------------------------------------------- 1 | package libwebp 2 | { 3 | import flash.external.ExtensionContext; 4 | 5 | internal class Webp 6 | { 7 | static internal var _context:ExtensionContext = null; 8 | static internal function get context():ExtensionContext 9 | { 10 | const contextId:String = 'webp'; 11 | if (_context === null) _context = ExtensionContext.createExtensionContext(contextId, ''); 12 | if (_context === null) throw(new Error("Can't get ExtensionContext: '" + contextId + "'")); 13 | return _context; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /ane/build_android.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | PUSHD jni 4 | CALL C:\Development\android-ndk-r6\ndk-build.cmd 5 | POPD 6 | COPY /Y libs\armeabi\libwebp_extension.so android\libAndroid.so 7 | RD /S /Q libs 8 | 9 | EXIT /B 10 | 11 | 12 | :: C:\Development\Android NDK\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin 13 | :: arm-linux-androideabi-g++ 14 | :: -Iinclude -IC:\Development\Android NDK/sources/cpufeatures \ 15 | :: --sysroot=C:\Development\Android NDK/platforms/android-5/arch-arm 16 | :: -IC:\Development\Android NDK/sources/cxx-stl/gnu-libstdc++/4.4.3/include 17 | :: -IC:\Development\Android NDK/sources/cxx-stl/gnu-libstdc++/4.4.3/libs/armeabi/include 18 | :: -DHXCPP_VISIT_ALLOCS 19 | :: -IC:\HaxeToolkit\haxe\lib\hxcpp\3,0,2//include 20 | :: -Iinclude 21 | :: -fpic -fvisibility=hidden -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums 22 | :: -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -D_LINUX_STDDEF_H 23 | :: -Wno-psabi 24 | :: -march=armv5te 25 | :: -mtune=xscale -msoft-float 26 | :: -fomit-frame-pointer -fexceptions -fno-strict-aliasing -finline-limit=10000 27 | :: -DANDROID -Wa,--noexecstack -O2 -DNDEBUG -c -x c 28 | :: ./webp/dec/frame.c -oobj/android/webp/dec/frame.obj 29 | :: C:/Development/Android NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld.exe 30 | :: C:\Development\Android NDK\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\arm-linux-androideabi\bin\ld.exe 31 | :: "C:\Development\Android NDK\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\arm-linux-androideabi\bin\gcc.exe" 32 | 33 | SET AIRSDK=c:\dev\airsdk 34 | 35 | SET LIBWEBP= 36 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dec/alpha.c 37 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dec/buffer.c 38 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dec/frame.c 39 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dec/idec.c 40 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dec/io.c 41 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dec/layer.c 42 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dec/quant.c 43 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dec/tree.c 44 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dec/vp8.c 45 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dec/vp8l.c 46 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dec/webp.c 47 | SET LIBWEBP=%LIBWEBP% 48 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dsp/cpu.c 49 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dsp/dec.c 50 | :: SET LIBWEBP=%LIBWEBP% ../libwebp/src/dsp/dec_sse2.c 51 | :: SET LIBWEBP=%LIBWEBP% ../libwebp/src/dsp/upsampling_sse2.c 52 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/dsp/enc_sse2.c 53 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/dsp/enc.c 54 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dsp/lossless.c 55 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dsp/upsampling.c 56 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/dsp/yuv.c 57 | SET LIBWEBP=%LIBWEBP% 58 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/alpha.c 59 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/analysis.c 60 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/backward_references.c 61 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/config.c 62 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/cost.c 63 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/filter.c 64 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/frame.c 65 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/histogram.c 66 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/iterator.c 67 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/layer.c 68 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/picture.c 69 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/quant.c 70 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/syntax.c 71 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/tree.c 72 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/vp8l.c 73 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/webpenc.c 74 | REM SET LIBWEBP=%LIBWEBP% ../libwebp/src/enc/token.c 75 | SET LIBWEBP=%LIBWEBP% 76 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/demux/demux.c 77 | SET LIBWEBP=%LIBWEBP% 78 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/mux/muxedit.c 79 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/mux/muxinternal.c 80 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/mux/muxread.c 81 | SET LIBWEBP=%LIBWEBP% 82 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/utils/bit_reader.c 83 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/utils/bit_writer.c 84 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/utils/color_cache.c 85 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/utils/filters.c 86 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/utils/huffman.c 87 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/utils/huffman_encode.c 88 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/utils/quant_levels.c 89 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/utils/rescaler.c 90 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/utils/thread.c 91 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/utils/utils.c 92 | SET LIBWEBP=%LIBWEBP% ../libwebp/src/utils/quant_levels_dec.c 93 | 94 | SET WEBPEXT= 95 | SET WEBPEXT=%WEBPEXT% webp_extension.c 96 | 97 | 98 | SET GCC="C:\Development\Android NDK\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\bin\arm-linux-androideabi-gcc" 99 | ::SET GCC="C:\Development\Android NDK\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\arm-linux-androideabi\bin\gcc.exe" 100 | 101 | SET GCC_EXTRA= 102 | SET GCC_EXTRA=%GCC_EXTRA% -Iinclude -I"C:\Development\Android NDK/sources/cpufeatures" 103 | SET GCC_EXTRA=%GCC_EXTRA% --sysroot="C:\Development\Android NDK/platforms/android-5/arch-arm" 104 | SET GCC_EXTRA=%GCC_EXTRA% -I"C:\Development\Android NDK/sources/cxx-stl/gnu-libstdc++/4.4.3/include" 105 | SET GCC_EXTRA=%GCC_EXTRA% -I"C:\Development\Android NDK/sources/cxx-stl/gnu-libstdc++/4.4.3/libs/armeabi/include" 106 | SET GCC_EXTRA=%GCC_EXTRA% -I"%AIRSDK%/include" 107 | ::SET GCC_EXTRA=%GCC_EXTRA% -l"%AIRSDK%\lib\android\FlashRuntimeExtensions.so" 108 | SET GCC_EXTRA=%GCC_EXTRA% -Iinclude 109 | :: SET GCC_EXTRA=%GCC_EXTRA% -fpic -fvisibility=hidden -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums 110 | SET GCC_EXTRA=%GCC_EXTRA% -fpic 111 | SET GCC_EXTRA=%GCC_EXTRA% -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -D_LINUX_STDDEF_H 112 | SET GCC_EXTRA=%GCC_EXTRA% -DENABLE_PROTECTION=OFF 113 | SET GCC_EXTRA=%GCC_EXTRA% -Wno-psabi 114 | SET GCC_EXTRA=%GCC_EXTRA% -march=armv5te 115 | SET GCC_EXTRA=%GCC_EXTRA% -mtune=xscale -msoft-float 116 | SET GCC_EXTRA=%GCC_EXTRA% -fomit-frame-pointer -fexceptions -fno-strict-aliasing -finline-limit=10000 117 | SET GCC_EXTRA=%GCC_EXTRA% -DANDROID -Wa,--noexecstack -O2 -DNDEBUG -x c 118 | 119 | 120 | %GCC% -shared -o android/libAndroid.so %GCC_EXTRA% %LIBWEBP% %WEBPEXT% 121 | -------------------------------------------------------------------------------- /ane/build_android.sh: -------------------------------------------------------------------------------- 1 | pushd jni 2 | /opt/android-ndk/ndk-build 3 | popd 4 | cp libs/armeabi/libwebp_extension.so android/libAndroid.so 5 | rm -rf libs 6 | -------------------------------------------------------------------------------- /ane/default.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system use, 7 | # "build.properties", and override values to adapt the script to your 8 | # project structure. 9 | 10 | # Project target. 11 | target=android-8 12 | -------------------------------------------------------------------------------- /ane/default/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ane/default/library.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/ane/default/library.swf -------------------------------------------------------------------------------- /ane/extension.xml: -------------------------------------------------------------------------------- 1 | 2 | webp 3 | 1.0.0 4 | 5 | 6 | 7 | libWindows.dll 8 | initializer 9 | finalizer 10 | 11 | 12 | 13 | 14 | libMacOS.framework 15 | initializer 16 | finalizer 17 | 18 | 19 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /ane/ios/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ane/jni/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | LOCAL_MODULE := prebuild-FlashRuntimeExtensions 5 | LOCAL_SRC_FILES := external/lib/FlashRuntimeExtensions.so 6 | include $(PREBUILT_SHARED_LIBRARY) 7 | 8 | include $(CLEAR_VARS) 9 | LOCAL_MODULE := webp_extension 10 | APP_ABI := armeabi-v7a 11 | #APP_ABI := armeabi armeabi-v7a x86 mips 12 | ARCH := $(APP_ABI) 13 | APP_OPTIM := release 14 | LOCAL_CFLAGS=-ffast-math -O3 15 | LOCAL_CFLAGS += -DNDEBUG 16 | LOCAL_SRC_FILES := ../../libwebp/src/dec/alpha.c ../../libwebp/src/dec/buffer.c ../../libwebp/src/dec/frame.c ../../libwebp/src/dec/idec.c ../../libwebp/src/dec/io.c ../../libwebp/src/dec/quant.c ../../libwebp/src/dec/tree.c ../../libwebp/src/dec/vp8.c ../../libwebp/src/dec/vp8l.c ../../libwebp/src/dec/webp.c ../../libwebp/src/dsp/cpu.c ../../libwebp/src/dsp/dec_clip_tables.c ../../libwebp/src/demux/demux.c ../../libwebp/src/utils/bit_reader.c ../../libwebp/src/utils/color_cache.c ../../libwebp/src/utils/filters.c ../../libwebp/src/utils/huffman.c ../../libwebp/src/utils/rescaler.c ../../libwebp/src/utils/thread.c ../../libwebp/src/utils/random.c ../../libwebp/src/utils/utils.c ../../libwebp/src/utils/quant_levels_dec.c ../../libwebp/src/dsp/alpha_processing.c ../../libwebp/src/dsp/dec.c ../../libwebp/src/dsp/lossless.c ../../libwebp/src/dsp/upsampling.c ../../libwebp/src/dsp/yuv.c ../../libwebp/src/dsp/enc.c ../../libwebp/src/enc/alpha.c ../../libwebp/src/enc/analysis.c ../../libwebp/src/enc/backward_references.c ../../libwebp/src/enc/picture_csp.c ../../libwebp/src/enc/config.c ../../libwebp/src/enc/cost.c ../../libwebp/src/enc/filter.c ../../libwebp/src/enc/frame.c ../../libwebp/src/enc/histogram.c ../../libwebp/src/enc/iterator.c ../../libwebp/src/enc/picture.c ../../libwebp/src/enc/quant.c ../../libwebp/src/enc/syntax.c ../../libwebp/src/enc/tree.c ../../libwebp/src/enc/vp8l.c ../../libwebp/src/enc/webpenc.c ../../libwebp/src/enc/token.c ../../libwebp/src/mux/muxedit.c ../../libwebp/src/mux/muxinternal.c ../../libwebp/src/mux/muxread.c ../../libwebp/src/utils/bit_writer.c ../../libwebp/src/utils/huffman_encode.c ../../libwebp/src/utils/quant_levels.c 17 | LOCAL_SHARED_LIBRARIES := prebuild-FlashRuntimeExtensions 18 | LOCAL_C_INCLUDES := external/include 19 | include $(BUILD_SHARED_LIBRARY) -------------------------------------------------------------------------------- /ane/jni/external/lib/FlashRuntimeExtensions.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/ane/jni/external/lib/FlashRuntimeExtensions.so -------------------------------------------------------------------------------- /ane/libs/armeabi/FlashRuntimeExtensions.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/ane/libs/armeabi/FlashRuntimeExtensions.so -------------------------------------------------------------------------------- /ane/mac/libMacOS.framework/libMacOS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/ane/mac/libMacOS.framework/libMacOS -------------------------------------------------------------------------------- /ane/mac/library.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/ane/mac/library.swf -------------------------------------------------------------------------------- /ane/test.p12: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/ane/test.p12 -------------------------------------------------------------------------------- /ane/windows/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /ane/windows/libWindows.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/ane/windows/libWindows.dll -------------------------------------------------------------------------------- /ane/windows/library.swf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/ane/windows/library.swf -------------------------------------------------------------------------------- /as3api.c: -------------------------------------------------------------------------------- 1 | #include "libwebp/src/webp/decode.h" 2 | #include 3 | #include "AS3/AS3.h" 4 | 5 | void DecodeWebp() __attribute__((used, 6 | annotate("as3import:flash.utils.ByteArray"), 7 | annotate("as3import:flash.display.BitmapData"), 8 | annotate("as3import:flash.geom.Rectangle"), 9 | annotate("as3sig:public function DecodeWebp(inputByteArray:ByteArray):BitmapData"), 10 | annotate("as3package:libwebp"))); 11 | 12 | void DecodeWebp() 13 | { 14 | int input_address; 15 | int input_length; 16 | uint8_t* output_pointer; 17 | int width, height; 18 | 19 | inline_as3( 20 | "var width:int, height:int, output_pointer:int;\n" 21 | "var input_length:int = inputByteArray.length;\n" 22 | "var input_address:int = CModule.malloc(input_length);\n" 23 | "inputByteArray.position = 0;\n" 24 | "CModule.writeBytes(input_address, input_length, inputByteArray);\n" 25 | "inputByteArray.position = 0;\n" 26 | : : 27 | ); 28 | 29 | AS3_GetScalarFromVar(input_address, input_address); 30 | AS3_GetScalarFromVar(input_length, input_length); 31 | 32 | //output_pointer = WebPDecodeARGB((const uint8_t*)input_address, (size_t)input_length, &width, &height); 33 | //output_pointer = WebPDecodeRGBA((const uint8_t*)input_address, (size_t)input_length, &width, &height); 34 | output_pointer = WebPDecodeBGRA((const uint8_t*)input_address, (size_t)input_length, &width, &height); 35 | 36 | inline_as3( 37 | "CModule.free(input_address);\n" 38 | : : 39 | ); 40 | 41 | if (output_pointer == NULL) { 42 | inline_as3( 43 | "return null;\n" 44 | : : 45 | ); 46 | } else { 47 | AS3_CopyScalarToVar(width, width); 48 | AS3_CopyScalarToVar(height, height); 49 | AS3_CopyScalarToVar(output_pointer, output_pointer); 50 | 51 | inline_as3( 52 | "ram_init.position = output_pointer;\n" 53 | "var bitmapData:BitmapData = new BitmapData(width, height);\n" 54 | "bitmapData.setPixels(new Rectangle(0, 0, width, height), ram_init);\n" 55 | : : 56 | ); 57 | 58 | /* 59 | inline_as3( 60 | "var outputByteArray:ByteArray = new ByteArray();\n" 61 | "CModule.readBytes(output_pointer, width * height * 4, outputByteArray);\n" 62 | "outputByteArray.position = 0;\n" 63 | "var bitmapData:BitmapData = new BitmapData(width, height);\n" 64 | "bitmapData.setPixels(new Rectangle(0, 0, width, height), outputByteArray);\n" 65 | : : 66 | ); 67 | */ 68 | 69 | free(output_pointer); 70 | 71 | inline_as3( 72 | "return bitmapData;\n" 73 | : : 74 | ); 75 | } 76 | } 77 | 78 | int main() { AS3_GoAsync(); } 79 | -------------------------------------------------------------------------------- /as3api_encode.c: -------------------------------------------------------------------------------- 1 | #include "libwebp/src/webp/decode.h" 2 | #include 3 | #include "AS3/AS3.h" 4 | 5 | typedef struct 6 | { 7 | uint8_t B, G, R, A; 8 | } BGRA; 9 | 10 | void EncodeWebp() __attribute__((used, 11 | annotate("as3import:flash.utils.ByteArray"), 12 | annotate("as3import:flash.utils.Endian"), 13 | annotate("as3import:flash.display.BitmapData"), 14 | annotate("as3import:flash.geom.Rectangle"), 15 | annotate("as3sig:public function EncodeWebp(inputBitmapData:BitmapData, quality_factor:Number):ByteArray"), 16 | annotate("as3package:libwebp"))); 17 | 18 | void EncodeWebp() 19 | { 20 | size_t output_size; 21 | uint8_t* input_address; 22 | BGRA* pointer; 23 | BGRA* pointer_end; 24 | uint8_t* output_pointer; 25 | int width; 26 | int height; 27 | double quality_factor; 28 | BGRA v, v2; 29 | int size; 30 | int n; 31 | 32 | inline_as3( 33 | "var output_pointer:int, output_size:int;\n" 34 | "var width:int = inputBitmapData.width;\n" 35 | "var height:int = inputBitmapData.height;\n" 36 | "var input_length:int = width * height * 4;\n" 37 | "var input_address:int = CModule.malloc(input_length);\n" 38 | "var inputByteArray:ByteArray = inputBitmapData.getPixels(new Rectangle(0, 0, width, height));\n" 39 | "inputByteArray.endian = Endian.LITTLE_ENDIAN;\n" 40 | "inputByteArray.position = 0;\n" 41 | "CModule.writeBytes(input_address, input_length, inputByteArray);\n" 42 | "inputByteArray.position = 0;\n" 43 | : : 44 | ); 45 | 46 | AS3_GetScalarFromVar(input_address, input_address); 47 | AS3_GetScalarFromVar(width, width); 48 | AS3_GetScalarFromVar(height, height); 49 | AS3_GetScalarFromVar(quality_factor, quality_factor); 50 | 51 | pointer = (BGRA *)input_address; 52 | pointer_end = pointer + (width * height); 53 | for (; pointer < pointer_end; pointer++) { 54 | v = *pointer; 55 | v2.B = v.A; 56 | v2.G = v.R; 57 | v2.R = v.G; 58 | v2.A = v.B; 59 | *pointer = v2; 60 | } 61 | 62 | output_size = WebPEncodeBGRA(input_address, width, height, width * 4, (float)quality_factor, &output_pointer); 63 | //output_size = WebPEncodeARGB(input_address, width, height, width * 4, (float)quality_factor, &output_pointer); 64 | 65 | free(input_address); 66 | 67 | if (output_size == 0) { 68 | inline_as3( 69 | "return null;\n" 70 | : : 71 | ); 72 | } else { 73 | AS3_CopyScalarToVar(output_pointer, output_pointer); 74 | AS3_CopyScalarToVar(output_size, output_size); 75 | 76 | inline_as3( 77 | "var outputByteArray:ByteArray = new ByteArray();\n" 78 | "outputByteArray.endian = Endian.LITTLE_ENDIAN;\n" 79 | "CModule.readBytes(output_pointer, output_size, outputByteArray);\n" 80 | "outputByteArray.position = 0;\n" 81 | : : 82 | ); 83 | 84 | free(output_pointer); 85 | 86 | inline_as3( 87 | "return outputByteArray;\n" 88 | : : 89 | ); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /as3libwebp.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /bin/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /bin/libwebp.ane: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/bin/libwebp.ane -------------------------------------------------------------------------------- /bin/libwebp.swc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/bin/libwebp.swc -------------------------------------------------------------------------------- /bin/libwebp_decode.swc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/bin/libwebp_decode.swc -------------------------------------------------------------------------------- /docs/Adobe Flash Platform - Building the native library.url: -------------------------------------------------------------------------------- 1 | [InternetShortcut] 2 | URL=http://help.adobe.com/en_US/air/extensions/WSf268776665d7970d-2e74ffb4130044f3619-7fff.html#WSdb11516da818ea8d49ce0fe713341ed67cf-8000 3 | -------------------------------------------------------------------------------- /docs/ane-lab - AIR Native Extension is Lab codes - Google Project Hosting.url: -------------------------------------------------------------------------------- 1 | [InternetShortcut] 2 | URL=https://code.google.com/p/ane-lab/ 3 | -------------------------------------------------------------------------------- /html-template/history/history.css: -------------------------------------------------------------------------------- 1 | /* This CSS stylesheet defines styles used by required elements in a flex application page that supports browser history */ 2 | 3 | #ie_historyFrame { width: 0px; height: 0px; display:none } 4 | #firefox_anchorDiv { width: 0px; height: 0px; display:none } 5 | #safari_formDiv { width: 0px; height: 0px; display:none } 6 | #safari_rememberDiv { width: 0px; height: 0px; display:none } 7 | -------------------------------------------------------------------------------- /html-template/history/historyFrame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 27 | Hidden frame for Browser History support. 28 | 29 | 30 | -------------------------------------------------------------------------------- /html-template/index.template.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 13 | 14 | ${title} 15 | 16 | 17 | 23 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 61 | 62 | 63 | 67 | 68 | 69 | To view this page ensure that Adobe Flash Player version 70 | 0.0.0 or greater is installed. 71 | 72 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | Either scripts and active content are not permitted to run or Adobe Flash Player version 96 | 0.0.0 or greater is not installed. 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /libwebp/src/Makefile.am: -------------------------------------------------------------------------------- 1 | # The mux and demux libraries depend on libwebp, thus the '.' to force the 2 | # build order so it's available to them. 3 | SUBDIRS = dec enc dsp utils . 4 | if WANT_MUX 5 | SUBDIRS += mux 6 | endif 7 | if WANT_DEMUX 8 | SUBDIRS += demux 9 | endif 10 | 11 | lib_LTLIBRARIES = libwebp.la 12 | 13 | if BUILD_LIBWEBPDECODER 14 | lib_LTLIBRARIES += libwebpdecoder.la 15 | endif 16 | 17 | common_HEADERS = 18 | common_HEADERS += webp/decode.h 19 | common_HEADERS += webp/types.h 20 | commondir = $(includedir)/webp 21 | 22 | libwebp_la_SOURCES = 23 | libwebpinclude_HEADERS = 24 | libwebpinclude_HEADERS += webp/encode.h 25 | noinst_HEADERS = 26 | noinst_HEADERS += webp/format_constants.h 27 | 28 | libwebp_la_LIBADD = 29 | libwebp_la_LIBADD += dec/libwebpdecode.la 30 | libwebp_la_LIBADD += dsp/libwebpdsp.la 31 | libwebp_la_LIBADD += enc/libwebpencode.la 32 | libwebp_la_LIBADD += utils/libwebputils.la 33 | 34 | # Use '-no-undefined' to declare that libwebp does not depend on any libraries 35 | # other than the ones listed on the command line, i.e., after linking, it will 36 | # not have unresolved symbols. Some platforms (Windows among them) require all 37 | # symbols in shared libraries to be resolved at library creation. 38 | libwebp_la_LDFLAGS = -no-undefined -version-info 5:2:0 39 | libwebpincludedir = $(includedir)/webp 40 | pkgconfig_DATA = libwebp.pc 41 | 42 | if BUILD_LIBWEBPDECODER 43 | libwebpdecoder_la_SOURCES = 44 | 45 | libwebpdecoder_la_LIBADD = 46 | libwebpdecoder_la_LIBADD += dec/libwebpdecode.la 47 | libwebpdecoder_la_LIBADD += dsp/libwebpdspdecode.la 48 | libwebpdecoder_la_LIBADD += utils/libwebputilsdecode.la 49 | 50 | libwebpdecoder_la_LDFLAGS = -no-undefined -version-info 1:2:0 51 | pkgconfig_DATA += libwebpdecoder.pc 52 | endif 53 | 54 | ${pkgconfig_DATA}: ${top_builddir}/config.status 55 | -------------------------------------------------------------------------------- /libwebp/src/dec/Makefile.am: -------------------------------------------------------------------------------- 1 | noinst_LTLIBRARIES = libwebpdecode.la 2 | 3 | libwebpdecode_la_SOURCES = 4 | libwebpdecode_la_SOURCES += alpha.c 5 | libwebpdecode_la_SOURCES += alphai.h 6 | libwebpdecode_la_SOURCES += buffer.c 7 | libwebpdecode_la_SOURCES += decode_vp8.h 8 | libwebpdecode_la_SOURCES += frame.c 9 | libwebpdecode_la_SOURCES += idec.c 10 | libwebpdecode_la_SOURCES += io.c 11 | libwebpdecode_la_SOURCES += quant.c 12 | libwebpdecode_la_SOURCES += tree.c 13 | libwebpdecode_la_SOURCES += vp8.c 14 | libwebpdecode_la_SOURCES += vp8i.h 15 | libwebpdecode_la_SOURCES += vp8l.c 16 | libwebpdecode_la_SOURCES += vp8li.h 17 | libwebpdecode_la_SOURCES += webp.c 18 | libwebpdecode_la_SOURCES += webpi.h 19 | 20 | libwebpdecodeinclude_HEADERS = 21 | libwebpdecodeinclude_HEADERS += ../webp/decode.h 22 | libwebpdecodeinclude_HEADERS += ../webp/types.h 23 | noinst_HEADERS = 24 | noinst_HEADERS += ../webp/format_constants.h 25 | 26 | libwebpdecode_la_CPPFLAGS = $(USE_EXPERIMENTAL_CODE) 27 | libwebpdecodeincludedir = $(includedir)/webp 28 | -------------------------------------------------------------------------------- /libwebp/src/dec/alpha.c: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Alpha-plane decompression. 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #include 15 | #include "./alphai.h" 16 | #include "./vp8i.h" 17 | #include "./vp8li.h" 18 | #include "../utils/quant_levels_dec.h" 19 | #include "../utils/utils.h" 20 | #include "../webp/format_constants.h" 21 | 22 | //------------------------------------------------------------------------------ 23 | // ALPHDecoder object. 24 | 25 | ALPHDecoder* ALPHNew(void) { 26 | ALPHDecoder* const dec = (ALPHDecoder*)WebPSafeCalloc(1ULL, sizeof(*dec)); 27 | return dec; 28 | } 29 | 30 | void ALPHDelete(ALPHDecoder* const dec) { 31 | if (dec != NULL) { 32 | VP8LDelete(dec->vp8l_dec_); 33 | dec->vp8l_dec_ = NULL; 34 | WebPSafeFree(dec); 35 | } 36 | } 37 | 38 | //------------------------------------------------------------------------------ 39 | // Decoding. 40 | 41 | // Initialize alpha decoding by parsing the alpha header and decoding the image 42 | // header for alpha data stored using lossless compression. 43 | // Returns false in case of error in alpha header (data too short, invalid 44 | // compression method or filter, error in lossless header data etc). 45 | static int ALPHInit(ALPHDecoder* const dec, const uint8_t* data, 46 | size_t data_size, int width, int height, uint8_t* output) { 47 | int ok = 0; 48 | const uint8_t* const alpha_data = data + ALPHA_HEADER_LEN; 49 | const size_t alpha_data_size = data_size - ALPHA_HEADER_LEN; 50 | int rsrv; 51 | 52 | assert(width > 0 && height > 0); 53 | assert(data != NULL && output != NULL); 54 | 55 | dec->width_ = width; 56 | dec->height_ = height; 57 | 58 | if (data_size <= ALPHA_HEADER_LEN) { 59 | return 0; 60 | } 61 | 62 | dec->method_ = (data[0] >> 0) & 0x03; 63 | dec->filter_ = (data[0] >> 2) & 0x03; 64 | dec->pre_processing_ = (data[0] >> 4) & 0x03; 65 | rsrv = (data[0] >> 6) & 0x03; 66 | if (dec->method_ < ALPHA_NO_COMPRESSION || 67 | dec->method_ > ALPHA_LOSSLESS_COMPRESSION || 68 | dec->filter_ >= WEBP_FILTER_LAST || 69 | dec->pre_processing_ > ALPHA_PREPROCESSED_LEVELS || 70 | rsrv != 0) { 71 | return 0; 72 | } 73 | 74 | if (dec->method_ == ALPHA_NO_COMPRESSION) { 75 | const size_t alpha_decoded_size = dec->width_ * dec->height_; 76 | ok = (alpha_data_size >= alpha_decoded_size); 77 | } else { 78 | assert(dec->method_ == ALPHA_LOSSLESS_COMPRESSION); 79 | ok = VP8LDecodeAlphaHeader(dec, alpha_data, alpha_data_size, output); 80 | } 81 | return ok; 82 | } 83 | 84 | // Decodes, unfilters and dequantizes *at least* 'num_rows' rows of alpha 85 | // starting from row number 'row'. It assumes that rows up to (row - 1) have 86 | // already been decoded. 87 | // Returns false in case of bitstream error. 88 | static int ALPHDecode(VP8Decoder* const dec, int row, int num_rows) { 89 | ALPHDecoder* const alph_dec = dec->alph_dec_; 90 | const int width = alph_dec->width_; 91 | const int height = alph_dec->height_; 92 | WebPUnfilterFunc unfilter_func = WebPUnfilters[alph_dec->filter_]; 93 | uint8_t* const output = dec->alpha_plane_; 94 | if (alph_dec->method_ == ALPHA_NO_COMPRESSION) { 95 | const size_t offset = row * width; 96 | const size_t num_pixels = num_rows * width; 97 | assert(dec->alpha_data_size_ >= ALPHA_HEADER_LEN + offset + num_pixels); 98 | memcpy(dec->alpha_plane_ + offset, 99 | dec->alpha_data_ + ALPHA_HEADER_LEN + offset, num_pixels); 100 | } else { // alph_dec->method_ == ALPHA_LOSSLESS_COMPRESSION 101 | assert(alph_dec->vp8l_dec_ != NULL); 102 | if (!VP8LDecodeAlphaImageStream(alph_dec, row + num_rows)) { 103 | return 0; 104 | } 105 | } 106 | 107 | if (unfilter_func != NULL) { 108 | unfilter_func(width, height, width, row, num_rows, output); 109 | } 110 | 111 | if (row + num_rows == dec->pic_hdr_.height_) { 112 | dec->is_alpha_decoded_ = 1; 113 | } 114 | return 1; 115 | } 116 | 117 | //------------------------------------------------------------------------------ 118 | // Main entry point. 119 | 120 | const uint8_t* VP8DecompressAlphaRows(VP8Decoder* const dec, 121 | int row, int num_rows) { 122 | const int width = dec->pic_hdr_.width_; 123 | const int height = dec->pic_hdr_.height_; 124 | 125 | if (row < 0 || num_rows <= 0 || row + num_rows > height) { 126 | return NULL; // sanity check. 127 | } 128 | 129 | if (row == 0) { 130 | // Initialize decoding. 131 | assert(dec->alpha_plane_ != NULL); 132 | dec->alph_dec_ = ALPHNew(); 133 | if (dec->alph_dec_ == NULL) return NULL; 134 | if (!ALPHInit(dec->alph_dec_, dec->alpha_data_, dec->alpha_data_size_, 135 | width, height, dec->alpha_plane_)) { 136 | ALPHDelete(dec->alph_dec_); 137 | dec->alph_dec_ = NULL; 138 | return NULL; 139 | } 140 | // if we allowed use of alpha dithering, check whether it's needed at all 141 | if (dec->alph_dec_->pre_processing_ != ALPHA_PREPROCESSED_LEVELS) { 142 | dec->alpha_dithering_ = 0; // disable dithering 143 | } else { 144 | num_rows = height; // decode everything in one pass 145 | } 146 | } 147 | 148 | if (!dec->is_alpha_decoded_) { 149 | int ok = 0; 150 | assert(dec->alph_dec_ != NULL); 151 | ok = ALPHDecode(dec, row, num_rows); 152 | if (ok && dec->alpha_dithering_ > 0) { 153 | ok = WebPDequantizeLevels(dec->alpha_plane_, width, height, 154 | dec->alpha_dithering_); 155 | } 156 | if (!ok || dec->is_alpha_decoded_) { 157 | ALPHDelete(dec->alph_dec_); 158 | dec->alph_dec_ = NULL; 159 | } 160 | if (!ok) return NULL; // Error. 161 | } 162 | 163 | // Return a pointer to the current decoded row. 164 | return dec->alpha_plane_ + row * width; 165 | } 166 | -------------------------------------------------------------------------------- /libwebp/src/dec/alphai.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Alpha decoder: internal header. 11 | // 12 | // Author: Urvang (urvang@google.com) 13 | 14 | #ifndef WEBP_DEC_ALPHAI_H_ 15 | #define WEBP_DEC_ALPHAI_H_ 16 | 17 | #include "./webpi.h" 18 | #include "../utils/filters.h" 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | struct VP8LDecoder; // Defined in dec/vp8li.h. 25 | 26 | typedef struct ALPHDecoder ALPHDecoder; 27 | struct ALPHDecoder { 28 | int width_; 29 | int height_; 30 | int method_; 31 | WEBP_FILTER_TYPE filter_; 32 | int pre_processing_; 33 | struct VP8LDecoder* vp8l_dec_; 34 | VP8Io io_; 35 | int use_8b_decode; // Although alpha channel requires only 1 byte per 36 | // pixel, sometimes VP8LDecoder may need to allocate 37 | // 4 bytes per pixel internally during decode. 38 | }; 39 | 40 | //------------------------------------------------------------------------------ 41 | // internal functions. Not public. 42 | 43 | // Allocates a new alpha decoder instance. 44 | ALPHDecoder* ALPHNew(void); 45 | 46 | // Clears and deallocates an alpha decoder instance. 47 | void ALPHDelete(ALPHDecoder* const dec); 48 | 49 | //------------------------------------------------------------------------------ 50 | 51 | #ifdef __cplusplus 52 | } // extern "C" 53 | #endif 54 | 55 | #endif /* WEBP_DEC_ALPHAI_H_ */ 56 | -------------------------------------------------------------------------------- /libwebp/src/dec/quant.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Quantizer initialization 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #include "./vp8i.h" 15 | 16 | static WEBP_INLINE int clip(int v, int M) { 17 | return v < 0 ? 0 : v > M ? M : v; 18 | } 19 | 20 | // Paragraph 14.1 21 | static const uint8_t kDcTable[128] = { 22 | 4, 5, 6, 7, 8, 9, 10, 10, 23 | 11, 12, 13, 14, 15, 16, 17, 17, 24 | 18, 19, 20, 20, 21, 21, 22, 22, 25 | 23, 23, 24, 25, 25, 26, 27, 28, 26 | 29, 30, 31, 32, 33, 34, 35, 36, 27 | 37, 37, 38, 39, 40, 41, 42, 43, 28 | 44, 45, 46, 46, 47, 48, 49, 50, 29 | 51, 52, 53, 54, 55, 56, 57, 58, 30 | 59, 60, 61, 62, 63, 64, 65, 66, 31 | 67, 68, 69, 70, 71, 72, 73, 74, 32 | 75, 76, 76, 77, 78, 79, 80, 81, 33 | 82, 83, 84, 85, 86, 87, 88, 89, 34 | 91, 93, 95, 96, 98, 100, 101, 102, 35 | 104, 106, 108, 110, 112, 114, 116, 118, 36 | 122, 124, 126, 128, 130, 132, 134, 136, 37 | 138, 140, 143, 145, 148, 151, 154, 157 38 | }; 39 | 40 | static const uint16_t kAcTable[128] = { 41 | 4, 5, 6, 7, 8, 9, 10, 11, 42 | 12, 13, 14, 15, 16, 17, 18, 19, 43 | 20, 21, 22, 23, 24, 25, 26, 27, 44 | 28, 29, 30, 31, 32, 33, 34, 35, 45 | 36, 37, 38, 39, 40, 41, 42, 43, 46 | 44, 45, 46, 47, 48, 49, 50, 51, 47 | 52, 53, 54, 55, 56, 57, 58, 60, 48 | 62, 64, 66, 68, 70, 72, 74, 76, 49 | 78, 80, 82, 84, 86, 88, 90, 92, 50 | 94, 96, 98, 100, 102, 104, 106, 108, 51 | 110, 112, 114, 116, 119, 122, 125, 128, 52 | 131, 134, 137, 140, 143, 146, 149, 152, 53 | 155, 158, 161, 164, 167, 170, 173, 177, 54 | 181, 185, 189, 193, 197, 201, 205, 209, 55 | 213, 217, 221, 225, 229, 234, 239, 245, 56 | 249, 254, 259, 264, 269, 274, 279, 284 57 | }; 58 | 59 | //------------------------------------------------------------------------------ 60 | // Paragraph 9.6 61 | 62 | void VP8ParseQuant(VP8Decoder* const dec) { 63 | VP8BitReader* const br = &dec->br_; 64 | const int base_q0 = VP8GetValue(br, 7); 65 | const int dqy1_dc = VP8Get(br) ? VP8GetSignedValue(br, 4) : 0; 66 | const int dqy2_dc = VP8Get(br) ? VP8GetSignedValue(br, 4) : 0; 67 | const int dqy2_ac = VP8Get(br) ? VP8GetSignedValue(br, 4) : 0; 68 | const int dquv_dc = VP8Get(br) ? VP8GetSignedValue(br, 4) : 0; 69 | const int dquv_ac = VP8Get(br) ? VP8GetSignedValue(br, 4) : 0; 70 | 71 | const VP8SegmentHeader* const hdr = &dec->segment_hdr_; 72 | int i; 73 | 74 | for (i = 0; i < NUM_MB_SEGMENTS; ++i) { 75 | int q; 76 | if (hdr->use_segment_) { 77 | q = hdr->quantizer_[i]; 78 | if (!hdr->absolute_delta_) { 79 | q += base_q0; 80 | } 81 | } else { 82 | if (i > 0) { 83 | dec->dqm_[i] = dec->dqm_[0]; 84 | continue; 85 | } else { 86 | q = base_q0; 87 | } 88 | } 89 | { 90 | VP8QuantMatrix* const m = &dec->dqm_[i]; 91 | m->y1_mat_[0] = kDcTable[clip(q + dqy1_dc, 127)]; 92 | m->y1_mat_[1] = kAcTable[clip(q + 0, 127)]; 93 | 94 | m->y2_mat_[0] = kDcTable[clip(q + dqy2_dc, 127)] * 2; 95 | // For all x in [0..284], x*155/100 is bitwise equal to (x*101581) >> 16. 96 | // The smallest precision for that is '(x*6349) >> 12' but 16 is a good 97 | // word size. 98 | m->y2_mat_[1] = (kAcTable[clip(q + dqy2_ac, 127)] * 101581) >> 16; 99 | if (m->y2_mat_[1] < 8) m->y2_mat_[1] = 8; 100 | 101 | m->uv_mat_[0] = kDcTable[clip(q + dquv_dc, 117)]; 102 | m->uv_mat_[1] = kAcTable[clip(q + dquv_ac, 127)]; 103 | 104 | m->uv_quant_ = q + dquv_ac; // for dithering strength evaluation 105 | } 106 | } 107 | } 108 | 109 | //------------------------------------------------------------------------------ 110 | 111 | -------------------------------------------------------------------------------- /libwebp/src/dec/vp8li.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Lossless decoder: internal header. 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | // Vikas Arora(vikaas.arora@gmail.com) 14 | 15 | #ifndef WEBP_DEC_VP8LI_H_ 16 | #define WEBP_DEC_VP8LI_H_ 17 | 18 | #include // for memcpy() 19 | #include "./webpi.h" 20 | #include "../utils/bit_reader.h" 21 | #include "../utils/color_cache.h" 22 | #include "../utils/huffman.h" 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | typedef enum { 29 | READ_DATA = 0, 30 | READ_HDR = 1, 31 | READ_DIM = 2 32 | } VP8LDecodeState; 33 | 34 | typedef struct VP8LTransform VP8LTransform; 35 | struct VP8LTransform { 36 | VP8LImageTransformType type_; // transform type. 37 | int bits_; // subsampling bits defining transform window. 38 | int xsize_; // transform window X index. 39 | int ysize_; // transform window Y index. 40 | uint32_t *data_; // transform data. 41 | }; 42 | 43 | typedef struct { 44 | int color_cache_size_; 45 | VP8LColorCache color_cache_; 46 | 47 | int huffman_mask_; 48 | int huffman_subsample_bits_; 49 | int huffman_xsize_; 50 | uint32_t *huffman_image_; 51 | int num_htree_groups_; 52 | HTreeGroup *htree_groups_; 53 | } VP8LMetadata; 54 | 55 | typedef struct VP8LDecoder VP8LDecoder; 56 | struct VP8LDecoder { 57 | VP8StatusCode status_; 58 | VP8LDecodeState action_; 59 | VP8LDecodeState state_; 60 | VP8Io *io_; 61 | 62 | const WebPDecBuffer *output_; // shortcut to io->opaque->output 63 | 64 | uint32_t *pixels_; // Internal data: either uint8_t* for alpha 65 | // or uint32_t* for BGRA. 66 | uint32_t *argb_cache_; // Scratch buffer for temporary BGRA storage. 67 | 68 | VP8LBitReader br_; 69 | 70 | int width_; 71 | int height_; 72 | int last_row_; // last input row decoded so far. 73 | int last_pixel_; // last pixel decoded so far. However, it may 74 | // not be transformed, scaled and 75 | // color-converted yet. 76 | int last_out_row_; // last row output so far. 77 | 78 | VP8LMetadata hdr_; 79 | 80 | int next_transform_; 81 | VP8LTransform transforms_[NUM_TRANSFORMS]; 82 | // or'd bitset storing the transforms types. 83 | uint32_t transforms_seen_; 84 | 85 | uint8_t *rescaler_memory; // Working memory for rescaling work. 86 | WebPRescaler *rescaler; // Common rescaler for all channels. 87 | }; 88 | 89 | //------------------------------------------------------------------------------ 90 | // internal functions. Not public. 91 | 92 | struct ALPHDecoder; // Defined in dec/alphai.h. 93 | 94 | // in vp8l.c 95 | 96 | // Decodes image header for alpha data stored using lossless compression. 97 | // Returns false in case of error. 98 | int VP8LDecodeAlphaHeader(struct ALPHDecoder* const alph_dec, 99 | const uint8_t* const data, size_t data_size, 100 | uint8_t* const output); 101 | 102 | // Decodes *at least* 'last_row' rows of alpha. If some of the initial rows are 103 | // already decoded in previous call(s), it will resume decoding from where it 104 | // was paused. 105 | // Returns false in case of bitstream error. 106 | int VP8LDecodeAlphaImageStream(struct ALPHDecoder* const alph_dec, 107 | int last_row); 108 | 109 | // Allocates and initialize a new lossless decoder instance. 110 | VP8LDecoder* VP8LNew(void); 111 | 112 | // Decodes the image header. Returns false in case of error. 113 | int VP8LDecodeHeader(VP8LDecoder* const dec, VP8Io* const io); 114 | 115 | // Decodes an image. It's required to decode the lossless header before calling 116 | // this function. Returns false in case of error, with updated dec->status_. 117 | int VP8LDecodeImage(VP8LDecoder* const dec); 118 | 119 | // Resets the decoder in its initial state, reclaiming memory. 120 | // Preserves the dec->status_ value. 121 | void VP8LClear(VP8LDecoder* const dec); 122 | 123 | // Clears and deallocate a lossless decoder instance. 124 | void VP8LDelete(VP8LDecoder* const dec); 125 | 126 | //------------------------------------------------------------------------------ 127 | 128 | #ifdef __cplusplus 129 | } // extern "C" 130 | #endif 131 | 132 | #endif /* WEBP_DEC_VP8LI_H_ */ 133 | -------------------------------------------------------------------------------- /libwebp/src/dec/webpi.h: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Internal header: WebP decoding parameters and custom IO on buffer 11 | // 12 | // Author: somnath@google.com (Somnath Banerjee) 13 | 14 | #ifndef WEBP_DEC_WEBPI_H_ 15 | #define WEBP_DEC_WEBPI_H_ 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #include "../utils/rescaler.h" 22 | #include "./decode_vp8.h" 23 | 24 | //------------------------------------------------------------------------------ 25 | // WebPDecParams: Decoding output parameters. Transient internal object. 26 | 27 | typedef struct WebPDecParams WebPDecParams; 28 | typedef int (*OutputFunc)(const VP8Io* const io, WebPDecParams* const p); 29 | typedef int (*OutputRowFunc)(WebPDecParams* const p, int y_pos); 30 | 31 | struct WebPDecParams { 32 | WebPDecBuffer* output; // output buffer. 33 | uint8_t* tmp_y, *tmp_u, *tmp_v; // cache for the fancy upsampler 34 | // or used for tmp rescaling 35 | 36 | int last_y; // coordinate of the line that was last output 37 | const WebPDecoderOptions* options; // if not NULL, use alt decoding features 38 | // rescalers 39 | WebPRescaler scaler_y, scaler_u, scaler_v, scaler_a; 40 | void* memory; // overall scratch memory for the output work. 41 | 42 | OutputFunc emit; // output RGB or YUV samples 43 | OutputFunc emit_alpha; // output alpha channel 44 | OutputRowFunc emit_alpha_row; // output one line of rescaled alpha values 45 | }; 46 | 47 | // Should be called first, before any use of the WebPDecParams object. 48 | void WebPResetDecParams(WebPDecParams* const params); 49 | 50 | //------------------------------------------------------------------------------ 51 | // Header parsing helpers 52 | 53 | // Structure storing a description of the RIFF headers. 54 | typedef struct { 55 | const uint8_t* data; // input buffer 56 | size_t data_size; // input buffer size 57 | int have_all_data; // true if all data is known to be available 58 | size_t offset; // offset to main data chunk (VP8 or VP8L) 59 | const uint8_t* alpha_data; // points to alpha chunk (if present) 60 | size_t alpha_data_size; // alpha chunk size 61 | size_t compressed_size; // VP8/VP8L compressed data size 62 | size_t riff_size; // size of the riff payload (or 0 if absent) 63 | int is_lossless; // true if a VP8L chunk is present 64 | } WebPHeaderStructure; 65 | 66 | // Skips over all valid chunks prior to the first VP8/VP8L frame header. 67 | // Returns: VP8_STATUS_OK, VP8_STATUS_BITSTREAM_ERROR (invalid header/chunk), 68 | // VP8_STATUS_NOT_ENOUGH_DATA (partial input) or VP8_STATUS_UNSUPPORTED_FEATURE 69 | // in the case of non-decodable features (animation for instance). 70 | // In 'headers', compressed_size, offset, alpha_data, alpha_size, and lossless 71 | // fields are updated appropriately upon success. 72 | VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers); 73 | 74 | //------------------------------------------------------------------------------ 75 | // Misc utils 76 | 77 | // Initializes VP8Io with custom setup, io and teardown functions. The default 78 | // hooks will use the supplied 'params' as io->opaque handle. 79 | void WebPInitCustomIo(WebPDecParams* const params, VP8Io* const io); 80 | 81 | // Setup crop_xxx fields, mb_w and mb_h in io. 'src_colorspace' refers 82 | // to the *compressed* format, not the output one. 83 | int WebPIoInitFromOptions(const WebPDecoderOptions* const options, 84 | VP8Io* const io, WEBP_CSP_MODE src_colorspace); 85 | 86 | //------------------------------------------------------------------------------ 87 | // Internal functions regarding WebPDecBuffer memory (in buffer.c). 88 | // Don't really need to be externally visible for now. 89 | 90 | // Prepare 'buffer' with the requested initial dimensions width/height. 91 | // If no external storage is supplied, initializes buffer by allocating output 92 | // memory and setting up the stride information. Validate the parameters. Return 93 | // an error code in case of problem (no memory, or invalid stride / size / 94 | // dimension / etc.). If *options is not NULL, also verify that the options' 95 | // parameters are valid and apply them to the width/height dimensions of the 96 | // output buffer. This takes cropping / scaling / rotation into account. 97 | // Also incorporates the options->flip flag to flip the buffer parameters if 98 | // needed. 99 | VP8StatusCode WebPAllocateDecBuffer(int width, int height, 100 | const WebPDecoderOptions* const options, 101 | WebPDecBuffer* const buffer); 102 | 103 | // Flip buffer vertically by negating the various strides. 104 | VP8StatusCode WebPFlipBuffer(WebPDecBuffer* const buffer); 105 | 106 | // Copy 'src' into 'dst' buffer, making sure 'dst' is not marked as owner of the 107 | // memory (still held by 'src'). 108 | void WebPCopyDecBuffer(const WebPDecBuffer* const src, 109 | WebPDecBuffer* const dst); 110 | 111 | // Copy and transfer ownership from src to dst (beware of parameter order!) 112 | void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst); 113 | 114 | //------------------------------------------------------------------------------ 115 | 116 | #ifdef __cplusplus 117 | } // extern "C" 118 | #endif 119 | 120 | #endif /* WEBP_DEC_WEBPI_H_ */ 121 | -------------------------------------------------------------------------------- /libwebp/src/demux/Makefile.am: -------------------------------------------------------------------------------- 1 | lib_LTLIBRARIES = libwebpdemux.la 2 | 3 | libwebpdemux_la_SOURCES = 4 | libwebpdemux_la_SOURCES += demux.c 5 | 6 | libwebpdemuxinclude_HEADERS = 7 | libwebpdemuxinclude_HEADERS += ../webp/demux.h 8 | libwebpdemuxinclude_HEADERS += ../webp/mux_types.h 9 | libwebpdemuxinclude_HEADERS += ../webp/types.h 10 | 11 | libwebpdemux_la_LIBADD = ../libwebp.la 12 | libwebpdemux_la_LDFLAGS = -no-undefined -version-info 1:2:0 13 | libwebpdemuxincludedir = $(includedir)/webp 14 | pkgconfig_DATA = libwebpdemux.pc 15 | -------------------------------------------------------------------------------- /libwebp/src/demux/libwebpdemux.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: libwebpdemux 7 | Description: Library for parsing the WebP graphics format container 8 | Version: @PACKAGE_VERSION@ 9 | Requires: libwebp >= 0.2.0 10 | Cflags: -I${includedir} 11 | Libs: -L${libdir} -lwebpdemux 12 | -------------------------------------------------------------------------------- /libwebp/src/dsp/Makefile.am: -------------------------------------------------------------------------------- 1 | noinst_LTLIBRARIES = libwebpdsp.la libwebpdsp_avx2.la 2 | noinst_LTLIBRARIES += libwebpdsp_sse2.la libwebpdspdecode_sse2.la 3 | 4 | if BUILD_LIBWEBPDECODER 5 | noinst_LTLIBRARIES += libwebpdspdecode.la 6 | endif 7 | 8 | common_HEADERS = ../webp/types.h 9 | commondir = $(includedir)/webp 10 | 11 | COMMON_SOURCES = 12 | COMMON_SOURCES += alpha_processing.c 13 | COMMON_SOURCES += cpu.c 14 | COMMON_SOURCES += dec.c 15 | COMMON_SOURCES += dec_clip_tables.c 16 | COMMON_SOURCES += dec_mips32.c 17 | COMMON_SOURCES += dec_neon.c 18 | COMMON_SOURCES += dsp.h 19 | COMMON_SOURCES += lossless.c 20 | COMMON_SOURCES += lossless.h 21 | COMMON_SOURCES += lossless_mips32.c 22 | COMMON_SOURCES += lossless_neon.c 23 | COMMON_SOURCES += neon.h 24 | COMMON_SOURCES += upsampling.c 25 | COMMON_SOURCES += upsampling_neon.c 26 | COMMON_SOURCES += yuv.c 27 | COMMON_SOURCES += yuv.h 28 | COMMON_SOURCES += yuv_mips32.c 29 | 30 | ENC_SOURCES = 31 | ENC_SOURCES += enc.c 32 | ENC_SOURCES += enc_mips32.c 33 | ENC_SOURCES += enc_neon.c 34 | 35 | libwebpdsp_avx2_la_SOURCES = 36 | libwebpdsp_avx2_la_SOURCES += enc_avx2.c 37 | libwebpdsp_avx2_la_CPPFLAGS = $(libwebpdsp_la_CPPFLAGS) 38 | libwebpdsp_avx2_la_CFLAGS = $(AM_CFLAGS) $(AVX2_FLAGS) 39 | 40 | libwebpdspdecode_sse2_la_SOURCES = 41 | libwebpdspdecode_sse2_la_SOURCES += alpha_processing_sse2.c 42 | libwebpdspdecode_sse2_la_SOURCES += dec_sse2.c 43 | libwebpdspdecode_sse2_la_SOURCES += lossless_sse2.c 44 | libwebpdspdecode_sse2_la_SOURCES += upsampling_sse2.c 45 | libwebpdspdecode_sse2_la_SOURCES += yuv_sse2.c 46 | libwebpdspdecode_sse2_la_SOURCES += yuv_tables_sse2.h 47 | libwebpdspdecode_sse2_la_CPPFLAGS = $(libwebpdsp_sse2_la_CPPFLAGS) 48 | libwebpdspdecode_sse2_la_CFLAGS = $(libwebpdsp_sse2_la_CFLAGS) 49 | 50 | libwebpdsp_sse2_la_SOURCES = 51 | libwebpdsp_sse2_la_SOURCES += enc_sse2.c 52 | libwebpdsp_sse2_la_CPPFLAGS = $(libwebpdsp_la_CPPFLAGS) 53 | libwebpdsp_sse2_la_CFLAGS = $(AM_CFLAGS) $(SSE2_FLAGS) 54 | libwebpdsp_sse2_la_LIBADD = libwebpdspdecode_sse2.la 55 | 56 | libwebpdsp_la_SOURCES = $(COMMON_SOURCES) $(ENC_SOURCES) 57 | 58 | noinst_HEADERS = 59 | noinst_HEADERS += ../dec/decode_vp8.h 60 | noinst_HEADERS += ../webp/decode.h 61 | 62 | libwebpdsp_la_CPPFLAGS = $(USE_EXPERIMENTAL_CODE) $(USE_SWAP_16BIT_CSP) 63 | libwebpdsp_la_LDFLAGS = -lm 64 | libwebpdsp_la_LIBADD = libwebpdsp_avx2.la libwebpdsp_sse2.la 65 | 66 | if BUILD_LIBWEBPDECODER 67 | libwebpdspdecode_la_SOURCES = $(COMMON_SOURCES) 68 | 69 | libwebpdspdecode_la_CPPFLAGS = $(libwebpdsp_la_CPPFLAGS) 70 | libwebpdspdecode_la_LDFLAGS = $(libwebpdsp_la_LDFLAGS) 71 | libwebpdspdecode_la_LIBADD = libwebpdspdecode_sse2.la 72 | endif 73 | -------------------------------------------------------------------------------- /libwebp/src/dsp/alpha_processing_sse2.c: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Utilities for processing transparent channel. 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #include "./dsp.h" 15 | 16 | #if defined(WEBP_USE_SSE2) 17 | #include 18 | 19 | //------------------------------------------------------------------------------ 20 | 21 | static int ExtractAlpha(const uint8_t* argb, int argb_stride, 22 | int width, int height, 23 | uint8_t* alpha, int alpha_stride) { 24 | // alpha_and stores an 'and' operation of all the alpha[] values. The final 25 | // value is not 0xff if any of the alpha[] is not equal to 0xff. 26 | uint32_t alpha_and = 0xff; 27 | int i, j; 28 | const __m128i a_mask = _mm_set1_epi32(0xffu); // to preserve alpha 29 | const __m128i all_0xff = _mm_set_epi32(0, 0, ~0u, ~0u); 30 | __m128i all_alphas = all_0xff; 31 | 32 | // We must be able to access 3 extra bytes after the last written byte 33 | // 'src[4 * width - 4]', because we don't know if alpha is the first or the 34 | // last byte of the quadruplet. 35 | const int limit = (width - 1) & ~7; 36 | 37 | for (j = 0; j < height; ++j) { 38 | const __m128i* src = (const __m128i*)argb; 39 | for (i = 0; i < limit; i += 8) { 40 | // load 32 argb bytes 41 | const __m128i a0 = _mm_loadu_si128(src + 0); 42 | const __m128i a1 = _mm_loadu_si128(src + 1); 43 | const __m128i b0 = _mm_and_si128(a0, a_mask); 44 | const __m128i b1 = _mm_and_si128(a1, a_mask); 45 | const __m128i c0 = _mm_packs_epi32(b0, b1); 46 | const __m128i d0 = _mm_packus_epi16(c0, c0); 47 | // store 48 | _mm_storel_epi64((__m128i*)&alpha[i], d0); 49 | // accumulate eight alpha 'and' in parallel 50 | all_alphas = _mm_and_si128(all_alphas, d0); 51 | src += 2; 52 | } 53 | for (; i < width; ++i) { 54 | const uint32_t alpha_value = argb[4 * i]; 55 | alpha[i] = alpha_value; 56 | alpha_and &= alpha_value; 57 | } 58 | argb += argb_stride; 59 | alpha += alpha_stride; 60 | } 61 | // Combine the eight alpha 'and' into a 8-bit mask. 62 | alpha_and &= _mm_movemask_epi8(_mm_cmpeq_epi8(all_alphas, all_0xff)); 63 | return (alpha_and == 0xff); 64 | } 65 | 66 | #endif // WEBP_USE_SSE2 67 | 68 | //------------------------------------------------------------------------------ 69 | // Init function 70 | 71 | extern void WebPInitAlphaProcessingSSE2(void); 72 | 73 | void WebPInitAlphaProcessingSSE2(void) { 74 | #if defined(WEBP_USE_SSE2) 75 | WebPExtractAlpha = ExtractAlpha; 76 | #endif 77 | } 78 | -------------------------------------------------------------------------------- /libwebp/src/dsp/cpu.c: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // CPU detection 11 | // 12 | // Author: Christian Duvivier (cduvivier@google.com) 13 | 14 | #include "./dsp.h" 15 | 16 | #if defined(__ANDROID__) 17 | #include 18 | #endif 19 | 20 | //------------------------------------------------------------------------------ 21 | // SSE2 detection. 22 | // 23 | 24 | // apple/darwin gcc-4.0.1 defines __PIC__, but not __pic__ with -fPIC. 25 | #if (defined(__pic__) || defined(__PIC__)) && defined(__i386__) 26 | static WEBP_INLINE void GetCPUInfo(int cpu_info[4], int info_type) { 27 | __asm__ volatile ( 28 | "mov %%ebx, %%edi\n" 29 | "cpuid\n" 30 | "xchg %%edi, %%ebx\n" 31 | : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3]) 32 | : "a"(info_type)); 33 | } 34 | #elif defined(__i386__) || defined(__x86_64__) 35 | static WEBP_INLINE void GetCPUInfo(int cpu_info[4], int info_type) { 36 | __asm__ volatile ( 37 | "cpuid\n" 38 | : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3]) 39 | : "a"(info_type)); 40 | } 41 | #elif defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 150030729 // >= VS2008 SP1 42 | #define GetCPUInfo(info, type) __cpuidex(info, type, 0) // set ecx=0 43 | #elif defined(WEBP_MSC_SSE2) 44 | #define GetCPUInfo __cpuid 45 | #endif 46 | 47 | // NaCl has no support for xgetbv or the raw opcode. 48 | #if !defined(__native_client__) && (defined(__i386__) || defined(__x86_64__)) 49 | static WEBP_INLINE uint64_t xgetbv(void) { 50 | const uint32_t ecx = 0; 51 | uint32_t eax, edx; 52 | // Use the raw opcode for xgetbv for compatibility with older toolchains. 53 | __asm__ volatile ( 54 | ".byte 0x0f, 0x01, 0xd0\n" 55 | : "=a"(eax), "=d"(edx) : "c" (ecx)); 56 | return ((uint64_t)edx << 32) | eax; 57 | } 58 | #elif defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 160040219 // >= VS2010 SP1 59 | #define xgetbv() _xgetbv(0) 60 | #elif defined(_MSC_VER) && defined(_M_IX86) 61 | static WEBP_INLINE uint64_t xgetbv(void) { 62 | uint32_t eax_, edx_; 63 | __asm { 64 | xor ecx, ecx // ecx = 0 65 | // Use the raw opcode for xgetbv for compatibility with older toolchains. 66 | __asm _emit 0x0f __asm _emit 0x01 __asm _emit 0xd0 67 | mov eax_, eax 68 | mov edx_, edx 69 | } 70 | return ((uint64_t)edx_ << 32) | eax_; 71 | } 72 | #else 73 | #define xgetbv() 0U // no AVX for older x64 or unrecognized toolchains. 74 | #endif 75 | 76 | #if defined(__i386__) || defined(__x86_64__) || defined(WEBP_MSC_SSE2) 77 | static int x86CPUInfo(CPUFeature feature) { 78 | int cpu_info[4]; 79 | GetCPUInfo(cpu_info, 1); 80 | if (feature == kSSE2) { 81 | return 0 != (cpu_info[3] & 0x04000000); 82 | } 83 | if (feature == kSSE3) { 84 | return 0 != (cpu_info[2] & 0x00000001); 85 | } 86 | if (feature == kAVX) { 87 | // bits 27 (OSXSAVE) & 28 (256-bit AVX) 88 | if ((cpu_info[2] & 0x18000000) == 0x18000000) { 89 | // XMM state and YMM state enabled by the OS. 90 | return (xgetbv() & 0x6) == 0x6; 91 | } 92 | } 93 | if (feature == kAVX2) { 94 | if (x86CPUInfo(kAVX)) { 95 | GetCPUInfo(cpu_info, 7); 96 | return ((cpu_info[1] & 0x00000020) == 0x00000020); 97 | } 98 | } 99 | return 0; 100 | } 101 | VP8CPUInfo VP8GetCPUInfo = x86CPUInfo; 102 | #elif defined(WEBP_ANDROID_NEON) // NB: needs to be before generic NEON test. 103 | static int AndroidCPUInfo(CPUFeature feature) { 104 | const AndroidCpuFamily cpu_family = android_getCpuFamily(); 105 | const uint64_t cpu_features = android_getCpuFeatures(); 106 | if (feature == kNEON) { 107 | return (cpu_family == ANDROID_CPU_FAMILY_ARM && 108 | 0 != (cpu_features & ANDROID_CPU_ARM_FEATURE_NEON)); 109 | } 110 | return 0; 111 | } 112 | VP8CPUInfo VP8GetCPUInfo = AndroidCPUInfo; 113 | #elif defined(WEBP_USE_NEON) 114 | // define a dummy function to enable turning off NEON at runtime by setting 115 | // VP8DecGetCPUInfo = NULL 116 | static int armCPUInfo(CPUFeature feature) { 117 | (void)feature; 118 | return 1; 119 | } 120 | VP8CPUInfo VP8GetCPUInfo = armCPUInfo; 121 | #elif defined(WEBP_USE_MIPS32) 122 | static int mipsCPUInfo(CPUFeature feature) { 123 | (void)feature; 124 | return 1; 125 | } 126 | VP8CPUInfo VP8GetCPUInfo = mipsCPUInfo; 127 | #else 128 | VP8CPUInfo VP8GetCPUInfo = NULL; 129 | #endif 130 | 131 | -------------------------------------------------------------------------------- /libwebp/src/dsp/enc_avx2.c: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // AVX2 version of speed-critical encoding functions. 11 | 12 | #include "./dsp.h" 13 | 14 | #if defined(WEBP_USE_AVX2) 15 | 16 | #endif // WEBP_USE_AVX2 17 | 18 | //------------------------------------------------------------------------------ 19 | // Entry point 20 | 21 | extern void VP8EncDspInitAVX2(void); 22 | 23 | void VP8EncDspInitAVX2(void) { 24 | } 25 | -------------------------------------------------------------------------------- /libwebp/src/dsp/neon.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // NEON common code. 11 | 12 | #ifndef WEBP_DSP_NEON_H_ 13 | #define WEBP_DSP_NEON_H_ 14 | 15 | #include 16 | 17 | #include "./dsp.h" 18 | 19 | // Right now, some intrinsics functions seem slower, so we disable them 20 | // everywhere except aarch64 where the inline assembly is incompatible. 21 | #if defined(__aarch64__) 22 | #define USE_INTRINSICS // use intrinsics when possible 23 | #endif 24 | 25 | #define INIT_VECTOR2(v, a, b) do { \ 26 | v.val[0] = a; \ 27 | v.val[1] = b; \ 28 | } while (0) 29 | 30 | #define INIT_VECTOR3(v, a, b, c) do { \ 31 | v.val[0] = a; \ 32 | v.val[1] = b; \ 33 | v.val[2] = c; \ 34 | } while (0) 35 | 36 | #define INIT_VECTOR4(v, a, b, c, d) do { \ 37 | v.val[0] = a; \ 38 | v.val[1] = b; \ 39 | v.val[2] = c; \ 40 | v.val[3] = d; \ 41 | } while (0) 42 | 43 | // if using intrinsics, this flag avoids some functions that make gcc-4.6.3 44 | // crash ("internal compiler error: in immed_double_const, at emit-rtl."). 45 | // (probably similar to gcc.gnu.org/bugzilla/show_bug.cgi?id=48183) 46 | #if !(LOCAL_GCC_PREREQ(4,8) || defined(__aarch64__)) 47 | #define WORK_AROUND_GCC 48 | #endif 49 | 50 | static WEBP_INLINE int32x4x4_t Transpose4x4(const int32x4x4_t rows) { 51 | uint64x2x2_t row01, row23; 52 | 53 | row01.val[0] = vreinterpretq_u64_s32(rows.val[0]); 54 | row01.val[1] = vreinterpretq_u64_s32(rows.val[1]); 55 | row23.val[0] = vreinterpretq_u64_s32(rows.val[2]); 56 | row23.val[1] = vreinterpretq_u64_s32(rows.val[3]); 57 | // Transpose 64-bit values (there's no vswp equivalent) 58 | { 59 | const uint64x1_t row0h = vget_high_u64(row01.val[0]); 60 | const uint64x1_t row2l = vget_low_u64(row23.val[0]); 61 | const uint64x1_t row1h = vget_high_u64(row01.val[1]); 62 | const uint64x1_t row3l = vget_low_u64(row23.val[1]); 63 | row01.val[0] = vcombine_u64(vget_low_u64(row01.val[0]), row2l); 64 | row23.val[0] = vcombine_u64(row0h, vget_high_u64(row23.val[0])); 65 | row01.val[1] = vcombine_u64(vget_low_u64(row01.val[1]), row3l); 66 | row23.val[1] = vcombine_u64(row1h, vget_high_u64(row23.val[1])); 67 | } 68 | { 69 | const int32x4x2_t out01 = vtrnq_s32(vreinterpretq_s32_u64(row01.val[0]), 70 | vreinterpretq_s32_u64(row01.val[1])); 71 | const int32x4x2_t out23 = vtrnq_s32(vreinterpretq_s32_u64(row23.val[0]), 72 | vreinterpretq_s32_u64(row23.val[1])); 73 | int32x4x4_t out; 74 | out.val[0] = out01.val[0]; 75 | out.val[1] = out01.val[1]; 76 | out.val[2] = out23.val[0]; 77 | out.val[3] = out23.val[1]; 78 | return out; 79 | } 80 | } 81 | 82 | #endif // WEBP_DSP_NEON_H_ 83 | -------------------------------------------------------------------------------- /libwebp/src/dsp/yuv.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // YUV->RGB conversion functions 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #include "./yuv.h" 15 | 16 | #if defined(WEBP_YUV_USE_TABLE) 17 | 18 | static int done = 0; 19 | 20 | static WEBP_INLINE uint8_t clip(int v, int max_value) { 21 | return v < 0 ? 0 : v > max_value ? max_value : v; 22 | } 23 | 24 | int16_t VP8kVToR[256], VP8kUToB[256]; 25 | int32_t VP8kVToG[256], VP8kUToG[256]; 26 | uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN]; 27 | uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN]; 28 | 29 | void VP8YUVInit(void) { 30 | int i; 31 | if (done) { 32 | return; 33 | } 34 | #ifndef USE_YUVj 35 | for (i = 0; i < 256; ++i) { 36 | VP8kVToR[i] = (89858 * (i - 128) + YUV_HALF) >> YUV_FIX; 37 | VP8kUToG[i] = -22014 * (i - 128) + YUV_HALF; 38 | VP8kVToG[i] = -45773 * (i - 128); 39 | VP8kUToB[i] = (113618 * (i - 128) + YUV_HALF) >> YUV_FIX; 40 | } 41 | for (i = YUV_RANGE_MIN; i < YUV_RANGE_MAX; ++i) { 42 | const int k = ((i - 16) * 76283 + YUV_HALF) >> YUV_FIX; 43 | VP8kClip[i - YUV_RANGE_MIN] = clip(k, 255); 44 | VP8kClip4Bits[i - YUV_RANGE_MIN] = clip((k + 8) >> 4, 15); 45 | } 46 | #else 47 | for (i = 0; i < 256; ++i) { 48 | VP8kVToR[i] = (91881 * (i - 128) + YUV_HALF) >> YUV_FIX; 49 | VP8kUToG[i] = -22554 * (i - 128) + YUV_HALF; 50 | VP8kVToG[i] = -46802 * (i - 128); 51 | VP8kUToB[i] = (116130 * (i - 128) + YUV_HALF) >> YUV_FIX; 52 | } 53 | for (i = YUV_RANGE_MIN; i < YUV_RANGE_MAX; ++i) { 54 | const int k = i; 55 | VP8kClip[i - YUV_RANGE_MIN] = clip(k, 255); 56 | VP8kClip4Bits[i - YUV_RANGE_MIN] = clip((k + 8) >> 4, 15); 57 | } 58 | #endif 59 | 60 | done = 1; 61 | } 62 | 63 | #else 64 | 65 | void VP8YUVInit(void) {} 66 | 67 | #endif // WEBP_YUV_USE_TABLE 68 | 69 | //----------------------------------------------------------------------------- 70 | // Plain-C version 71 | 72 | #define ROW_FUNC(FUNC_NAME, FUNC, XSTEP) \ 73 | static void FUNC_NAME(const uint8_t* y, \ 74 | const uint8_t* u, const uint8_t* v, \ 75 | uint8_t* dst, int len) { \ 76 | const uint8_t* const end = dst + (len & ~1) * XSTEP; \ 77 | while (dst != end) { \ 78 | FUNC(y[0], u[0], v[0], dst); \ 79 | FUNC(y[1], u[0], v[0], dst + XSTEP); \ 80 | y += 2; \ 81 | ++u; \ 82 | ++v; \ 83 | dst += 2 * XSTEP; \ 84 | } \ 85 | if (len & 1) { \ 86 | FUNC(y[0], u[0], v[0], dst); \ 87 | } \ 88 | } \ 89 | 90 | // All variants implemented. 91 | ROW_FUNC(YuvToRgbRow, VP8YuvToRgb, 3) 92 | ROW_FUNC(YuvToBgrRow, VP8YuvToBgr, 3) 93 | ROW_FUNC(YuvToRgbaRow, VP8YuvToRgba, 4) 94 | ROW_FUNC(YuvToBgraRow, VP8YuvToBgra, 4) 95 | ROW_FUNC(YuvToArgbRow, VP8YuvToArgb, 4) 96 | ROW_FUNC(YuvToRgba4444Row, VP8YuvToRgba4444, 2) 97 | ROW_FUNC(YuvToRgb565Row, VP8YuvToRgb565, 2) 98 | 99 | #undef ROW_FUNC 100 | 101 | // Main call for processing a plane with a WebPSamplerRowFunc function: 102 | void WebPSamplerProcessPlane(const uint8_t* y, int y_stride, 103 | const uint8_t* u, const uint8_t* v, int uv_stride, 104 | uint8_t* dst, int dst_stride, 105 | int width, int height, WebPSamplerRowFunc func) { 106 | int j; 107 | for (j = 0; j < height; ++j) { 108 | func(y, u, v, dst, width); 109 | y += y_stride; 110 | if (j & 1) { 111 | u += uv_stride; 112 | v += uv_stride; 113 | } 114 | dst += dst_stride; 115 | } 116 | } 117 | 118 | //----------------------------------------------------------------------------- 119 | // Main call 120 | 121 | WebPSamplerRowFunc WebPSamplers[MODE_LAST]; 122 | 123 | extern void WebPInitSamplersSSE2(void); 124 | extern void WebPInitSamplersMIPS32(void); 125 | 126 | void WebPInitSamplers(void) { 127 | WebPSamplers[MODE_RGB] = YuvToRgbRow; 128 | WebPSamplers[MODE_RGBA] = YuvToRgbaRow; 129 | WebPSamplers[MODE_BGR] = YuvToBgrRow; 130 | WebPSamplers[MODE_BGRA] = YuvToBgraRow; 131 | WebPSamplers[MODE_ARGB] = YuvToArgbRow; 132 | WebPSamplers[MODE_RGBA_4444] = YuvToRgba4444Row; 133 | WebPSamplers[MODE_RGB_565] = YuvToRgb565Row; 134 | WebPSamplers[MODE_rgbA] = YuvToRgbaRow; 135 | WebPSamplers[MODE_bgrA] = YuvToBgraRow; 136 | WebPSamplers[MODE_Argb] = YuvToArgbRow; 137 | WebPSamplers[MODE_rgbA_4444] = YuvToRgba4444Row; 138 | 139 | // If defined, use CPUInfo() to overwrite some pointers with faster versions. 140 | if (VP8GetCPUInfo != NULL) { 141 | #if defined(WEBP_USE_SSE2) 142 | if (VP8GetCPUInfo(kSSE2)) { 143 | WebPInitSamplersSSE2(); 144 | } 145 | #endif // WEBP_USE_SSE2 146 | #if defined(WEBP_USE_MIPS32) 147 | if (VP8GetCPUInfo(kMIPS32)) { 148 | WebPInitSamplersMIPS32(); 149 | } 150 | #endif // WEBP_USE_MIPS32 151 | } 152 | } 153 | 154 | //----------------------------------------------------------------------------- 155 | -------------------------------------------------------------------------------- /libwebp/src/dsp/yuv_mips32.c: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // MIPS version of YUV to RGB upsampling functions. 11 | // 12 | // Author(s): Djordje Pesut (djordje.pesut@imgtec.com) 13 | // Jovan Zelincevic (jovan.zelincevic@imgtec.com) 14 | 15 | #include "./dsp.h" 16 | 17 | #if defined(WEBP_USE_MIPS32) 18 | 19 | #include "./yuv.h" 20 | 21 | //------------------------------------------------------------------------------ 22 | // simple point-sampling 23 | 24 | #define ROW_FUNC(FUNC_NAME, XSTEP, R, G, B, A) \ 25 | static void FUNC_NAME(const uint8_t* y, \ 26 | const uint8_t* u, const uint8_t* v, \ 27 | uint8_t* dst, int len) { \ 28 | int i, r, g, b; \ 29 | int temp0, temp1, temp2, temp3, temp4; \ 30 | for (i = 0; i < (len >> 1); i++) { \ 31 | temp1 = kVToR * v[0]; \ 32 | temp3 = kVToG * v[0]; \ 33 | temp2 = kUToG * u[0]; \ 34 | temp4 = kUToB * u[0]; \ 35 | temp0 = kYScale * y[0]; \ 36 | temp1 += kRCst; \ 37 | temp3 -= kGCst; \ 38 | temp2 += temp3; \ 39 | temp4 += kBCst; \ 40 | r = VP8Clip8(temp0 + temp1); \ 41 | g = VP8Clip8(temp0 - temp2); \ 42 | b = VP8Clip8(temp0 + temp4); \ 43 | temp0 = kYScale * y[1]; \ 44 | dst[R] = r; \ 45 | dst[G] = g; \ 46 | dst[B] = b; \ 47 | if (A) dst[A] = 0xff; \ 48 | r = VP8Clip8(temp0 + temp1); \ 49 | g = VP8Clip8(temp0 - temp2); \ 50 | b = VP8Clip8(temp0 + temp4); \ 51 | dst[R + XSTEP] = r; \ 52 | dst[G + XSTEP] = g; \ 53 | dst[B + XSTEP] = b; \ 54 | if (A) dst[A + XSTEP] = 0xff; \ 55 | y += 2; \ 56 | ++u; \ 57 | ++v; \ 58 | dst += 2 * XSTEP; \ 59 | } \ 60 | if (len & 1) { \ 61 | temp1 = kVToR * v[0]; \ 62 | temp3 = kVToG * v[0]; \ 63 | temp2 = kUToG * u[0]; \ 64 | temp4 = kUToB * u[0]; \ 65 | temp0 = kYScale * y[0]; \ 66 | temp1 += kRCst; \ 67 | temp3 -= kGCst; \ 68 | temp2 += temp3; \ 69 | temp4 += kBCst; \ 70 | r = VP8Clip8(temp0 + temp1); \ 71 | g = VP8Clip8(temp0 - temp2); \ 72 | b = VP8Clip8(temp0 + temp4); \ 73 | dst[R] = r; \ 74 | dst[G] = g; \ 75 | dst[B] = b; \ 76 | if (A) dst[A] = 0xff; \ 77 | } \ 78 | } 79 | 80 | ROW_FUNC(YuvToRgbRow, 3, 0, 1, 2, 0) 81 | ROW_FUNC(YuvToRgbaRow, 4, 0, 1, 2, 3) 82 | ROW_FUNC(YuvToBgrRow, 3, 2, 1, 0, 0) 83 | ROW_FUNC(YuvToBgraRow, 4, 2, 1, 0, 3) 84 | 85 | #undef ROW_FUNC 86 | 87 | #endif // WEBP_USE_MIPS32 88 | 89 | //------------------------------------------------------------------------------ 90 | 91 | extern void WebPInitSamplersMIPS32(void); 92 | 93 | void WebPInitSamplersMIPS32(void) { 94 | #if defined(WEBP_USE_MIPS32) 95 | WebPSamplers[MODE_RGB] = YuvToRgbRow; 96 | WebPSamplers[MODE_RGBA] = YuvToRgbaRow; 97 | WebPSamplers[MODE_BGR] = YuvToBgrRow; 98 | WebPSamplers[MODE_BGRA] = YuvToBgraRow; 99 | #endif // WEBP_USE_MIPS32 100 | } 101 | -------------------------------------------------------------------------------- /libwebp/src/enc/Makefile.am: -------------------------------------------------------------------------------- 1 | noinst_LTLIBRARIES = libwebpencode.la 2 | 3 | libwebpencode_la_SOURCES = 4 | libwebpencode_la_SOURCES += alpha.c 5 | libwebpencode_la_SOURCES += analysis.c 6 | libwebpencode_la_SOURCES += backward_references.c 7 | libwebpencode_la_SOURCES += config.c 8 | libwebpencode_la_SOURCES += cost.c 9 | libwebpencode_la_SOURCES += cost.h 10 | libwebpencode_la_SOURCES += filter.c 11 | libwebpencode_la_SOURCES += frame.c 12 | libwebpencode_la_SOURCES += histogram.c 13 | libwebpencode_la_SOURCES += iterator.c 14 | libwebpencode_la_SOURCES += picture.c 15 | libwebpencode_la_SOURCES += picture_csp.c 16 | libwebpencode_la_SOURCES += picture_psnr.c 17 | libwebpencode_la_SOURCES += picture_rescale.c 18 | libwebpencode_la_SOURCES += picture_tools.c 19 | libwebpencode_la_SOURCES += quant.c 20 | libwebpencode_la_SOURCES += syntax.c 21 | libwebpencode_la_SOURCES += token.c 22 | libwebpencode_la_SOURCES += tree.c 23 | libwebpencode_la_SOURCES += vp8enci.h 24 | libwebpencode_la_SOURCES += vp8l.c 25 | libwebpencode_la_SOURCES += webpenc.c 26 | 27 | libwebpencodeinclude_HEADERS = 28 | libwebpencodeinclude_HEADERS += ../webp/encode.h 29 | libwebpencodeinclude_HEADERS += ../webp/types.h 30 | noinst_HEADERS = 31 | noinst_HEADERS += ../webp/format_constants.h 32 | 33 | libwebpencode_la_LDFLAGS = -lm 34 | libwebpencode_la_CPPFLAGS = $(USE_EXPERIMENTAL_CODE) 35 | libwebpencodeincludedir = $(includedir)/webp 36 | -------------------------------------------------------------------------------- /libwebp/src/enc/config.c: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Coding tools configuration 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #include "../webp/encode.h" 15 | 16 | //------------------------------------------------------------------------------ 17 | // WebPConfig 18 | //------------------------------------------------------------------------------ 19 | 20 | int WebPConfigInitInternal(WebPConfig* config, 21 | WebPPreset preset, float quality, int version) { 22 | if (WEBP_ABI_IS_INCOMPATIBLE(version, WEBP_ENCODER_ABI_VERSION)) { 23 | return 0; // caller/system version mismatch! 24 | } 25 | if (config == NULL) return 0; 26 | 27 | config->quality = quality; 28 | config->target_size = 0; 29 | config->target_PSNR = 0.; 30 | config->method = 4; 31 | config->sns_strength = 50; 32 | config->filter_strength = 60; // mid-filtering 33 | config->filter_sharpness = 0; 34 | config->filter_type = 1; // default: strong (so U/V is filtered too) 35 | config->partitions = 0; 36 | config->segments = 4; 37 | config->pass = 1; 38 | config->show_compressed = 0; 39 | config->preprocessing = 0; 40 | config->autofilter = 0; 41 | config->partition_limit = 0; 42 | config->alpha_compression = 1; 43 | config->alpha_filtering = 1; 44 | config->alpha_quality = 100; 45 | config->lossless = 0; 46 | config->image_hint = WEBP_HINT_DEFAULT; 47 | config->emulate_jpeg_size = 0; 48 | config->thread_level = 0; 49 | config->low_memory = 0; 50 | 51 | // TODO(skal): tune. 52 | switch (preset) { 53 | case WEBP_PRESET_PICTURE: 54 | config->sns_strength = 80; 55 | config->filter_sharpness = 4; 56 | config->filter_strength = 35; 57 | config->preprocessing &= ~2; // no dithering 58 | break; 59 | case WEBP_PRESET_PHOTO: 60 | config->sns_strength = 80; 61 | config->filter_sharpness = 3; 62 | config->filter_strength = 30; 63 | config->preprocessing |= 2; 64 | break; 65 | case WEBP_PRESET_DRAWING: 66 | config->sns_strength = 25; 67 | config->filter_sharpness = 6; 68 | config->filter_strength = 10; 69 | break; 70 | case WEBP_PRESET_ICON: 71 | config->sns_strength = 0; 72 | config->filter_strength = 0; // disable filtering to retain sharpness 73 | config->preprocessing &= ~2; // no dithering 74 | break; 75 | case WEBP_PRESET_TEXT: 76 | config->sns_strength = 0; 77 | config->filter_strength = 0; // disable filtering to retain sharpness 78 | config->preprocessing &= ~2; // no dithering 79 | config->segments = 2; 80 | break; 81 | case WEBP_PRESET_DEFAULT: 82 | default: 83 | break; 84 | } 85 | return WebPValidateConfig(config); 86 | } 87 | 88 | int WebPValidateConfig(const WebPConfig* config) { 89 | if (config == NULL) return 0; 90 | if (config->quality < 0 || config->quality > 100) 91 | return 0; 92 | if (config->target_size < 0) 93 | return 0; 94 | if (config->target_PSNR < 0) 95 | return 0; 96 | if (config->method < 0 || config->method > 6) 97 | return 0; 98 | if (config->segments < 1 || config->segments > 4) 99 | return 0; 100 | if (config->sns_strength < 0 || config->sns_strength > 100) 101 | return 0; 102 | if (config->filter_strength < 0 || config->filter_strength > 100) 103 | return 0; 104 | if (config->filter_sharpness < 0 || config->filter_sharpness > 7) 105 | return 0; 106 | if (config->filter_type < 0 || config->filter_type > 1) 107 | return 0; 108 | if (config->autofilter < 0 || config->autofilter > 1) 109 | return 0; 110 | if (config->pass < 1 || config->pass > 10) 111 | return 0; 112 | if (config->show_compressed < 0 || config->show_compressed > 1) 113 | return 0; 114 | #if WEBP_ENCODER_ABI_VERSION > 0x0204 115 | if (config->preprocessing < 0 || config->preprocessing > 7) 116 | #else 117 | if (config->preprocessing < 0 || config->preprocessing > 3) 118 | #endif 119 | return 0; 120 | if (config->partitions < 0 || config->partitions > 3) 121 | return 0; 122 | if (config->partition_limit < 0 || config->partition_limit > 100) 123 | return 0; 124 | if (config->alpha_compression < 0) 125 | return 0; 126 | if (config->alpha_filtering < 0) 127 | return 0; 128 | if (config->alpha_quality < 0 || config->alpha_quality > 100) 129 | return 0; 130 | if (config->lossless < 0 || config->lossless > 1) 131 | return 0; 132 | if (config->image_hint >= WEBP_HINT_LAST) 133 | return 0; 134 | if (config->emulate_jpeg_size < 0 || config->emulate_jpeg_size > 1) 135 | return 0; 136 | if (config->thread_level < 0 || config->thread_level > 1) 137 | return 0; 138 | if (config->low_memory < 0 || config->low_memory > 1) 139 | return 0; 140 | return 1; 141 | } 142 | 143 | //------------------------------------------------------------------------------ 144 | 145 | #if WEBP_ENCODER_ABI_VERSION > 0x0202 146 | #define MAX_LEVEL 9 147 | 148 | // Mapping between -z level and -m / -q parameter settings. 149 | static const struct { 150 | uint8_t method_; 151 | uint8_t quality_; 152 | } kLosslessPresets[MAX_LEVEL + 1] = { 153 | { 0, 0 }, { 1, 20 }, { 2, 25 }, { 3, 30 }, { 3, 50 }, 154 | { 4, 50 }, { 4, 75 }, { 4, 90 }, { 5, 90 }, { 6, 100 } 155 | }; 156 | 157 | int WebPConfigLosslessPreset(WebPConfig* config, int level) { 158 | if (config == NULL || level < 0 || level > MAX_LEVEL) return 0; 159 | config->lossless = 1; 160 | config->method = kLosslessPresets[level].method_; 161 | config->quality = kLosslessPresets[level].quality_; 162 | return 1; 163 | } 164 | #endif 165 | 166 | //------------------------------------------------------------------------------ 167 | -------------------------------------------------------------------------------- /libwebp/src/enc/cost.h: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Cost tables for level and modes. 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #ifndef WEBP_ENC_COST_H_ 15 | #define WEBP_ENC_COST_H_ 16 | 17 | #include 18 | #include 19 | #include "./vp8enci.h" 20 | 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | // On-the-fly info about the current set of residuals. Handy to avoid 26 | // passing zillions of params. 27 | typedef struct { 28 | int first; 29 | int last; 30 | const int16_t* coeffs; 31 | 32 | int coeff_type; 33 | ProbaArray* prob; 34 | StatsArray* stats; 35 | CostArray* cost; 36 | } VP8Residual; 37 | 38 | void VP8InitResidual(int first, int coeff_type, 39 | VP8Encoder* const enc, VP8Residual* const res); 40 | 41 | typedef void (*VP8SetResidualCoeffsFunc)(const int16_t* const coeffs, 42 | VP8Residual* const res); 43 | extern VP8SetResidualCoeffsFunc VP8SetResidualCoeffs; 44 | 45 | void VP8SetResidualCoeffsInit(void); // must be called first 46 | 47 | int VP8RecordCoeffs(int ctx, const VP8Residual* const res); 48 | 49 | // approximate cost per level: 50 | extern const uint16_t VP8LevelFixedCosts[MAX_LEVEL + 1]; 51 | extern const uint16_t VP8EntropyCost[256]; // 8bit fixed-point log(p) 52 | 53 | // Cost of coding one event with probability 'proba'. 54 | static WEBP_INLINE int VP8BitCost(int bit, uint8_t proba) { 55 | return !bit ? VP8EntropyCost[proba] : VP8EntropyCost[255 - proba]; 56 | } 57 | 58 | // Cost calculation function. 59 | typedef int (*VP8GetResidualCostFunc)(int ctx0, const VP8Residual* const res); 60 | extern VP8GetResidualCostFunc VP8GetResidualCost; 61 | 62 | void VP8GetResidualCostInit(void); // must be called first 63 | 64 | // Level cost calculations 65 | extern const uint16_t VP8LevelCodes[MAX_VARIABLE_LEVEL][2]; 66 | void VP8CalculateLevelCosts(VP8Proba* const proba); 67 | static WEBP_INLINE int VP8LevelCost(const uint16_t* const table, int level) { 68 | return VP8LevelFixedCosts[level] 69 | + table[(level > MAX_VARIABLE_LEVEL) ? MAX_VARIABLE_LEVEL : level]; 70 | } 71 | 72 | // Mode costs 73 | extern const uint16_t VP8FixedCostsUV[4]; 74 | extern const uint16_t VP8FixedCostsI16[4]; 75 | extern const uint16_t VP8FixedCostsI4[NUM_BMODES][NUM_BMODES][NUM_BMODES]; 76 | 77 | //------------------------------------------------------------------------------ 78 | 79 | #ifdef __cplusplus 80 | } // extern "C" 81 | #endif 82 | 83 | #endif /* WEBP_ENC_COST_H_ */ 84 | -------------------------------------------------------------------------------- /libwebp/src/enc/histogram.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Author: Jyrki Alakuijala (jyrki@google.com) 11 | // 12 | // Models the histograms of literal and distance codes. 13 | 14 | #ifndef WEBP_ENC_HISTOGRAM_H_ 15 | #define WEBP_ENC_HISTOGRAM_H_ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "./backward_references.h" 24 | #include "../webp/format_constants.h" 25 | #include "../webp/types.h" 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | // A simple container for histograms of data. 32 | typedef struct { 33 | // literal_ contains green literal, palette-code and 34 | // copy-length-prefix histogram 35 | uint32_t* literal_; // Pointer to the allocated buffer for literal. 36 | uint32_t red_[NUM_LITERAL_CODES]; 37 | uint32_t blue_[NUM_LITERAL_CODES]; 38 | uint32_t alpha_[NUM_LITERAL_CODES]; 39 | // Backward reference prefix-code histogram. 40 | uint32_t distance_[NUM_DISTANCE_CODES]; 41 | int palette_code_bits_; 42 | double bit_cost_; // cached value of VP8LHistogramEstimateBits(this) 43 | double literal_cost_; // Cached values of dominant entropy costs: 44 | double red_cost_; // literal, red & blue. 45 | double blue_cost_; 46 | } VP8LHistogram; 47 | 48 | // Collection of histograms with fixed capacity, allocated as one 49 | // big memory chunk. Can be destroyed by calling WebPSafeFree(). 50 | typedef struct { 51 | int size; // number of slots currently in use 52 | int max_size; // maximum capacity 53 | VP8LHistogram** histograms; 54 | } VP8LHistogramSet; 55 | 56 | // Create the histogram. 57 | // 58 | // The input data is the PixOrCopy data, which models the literals, stop 59 | // codes and backward references (both distances and lengths). Also: if 60 | // palette_code_bits is >= 0, initialize the histogram with this value. 61 | void VP8LHistogramCreate(VP8LHistogram* const p, 62 | const VP8LBackwardRefs* const refs, 63 | int palette_code_bits); 64 | 65 | // Return the size of the histogram for a given palette_code_bits. 66 | int VP8LGetHistogramSize(int palette_code_bits); 67 | 68 | // Set the palette_code_bits and reset the stats. 69 | void VP8LHistogramInit(VP8LHistogram* const p, int palette_code_bits); 70 | 71 | // Collect all the references into a histogram (without reset) 72 | void VP8LHistogramStoreRefs(const VP8LBackwardRefs* const refs, 73 | VP8LHistogram* const histo); 74 | 75 | // Free the memory allocated for the histogram. 76 | void VP8LFreeHistogram(VP8LHistogram* const histo); 77 | 78 | // Free the memory allocated for the histogram set. 79 | void VP8LFreeHistogramSet(VP8LHistogramSet* const histo); 80 | 81 | // Allocate an array of pointer to histograms, allocated and initialized 82 | // using 'cache_bits'. Return NULL in case of memory error. 83 | VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits); 84 | 85 | // Allocate and initialize histogram object with specified 'cache_bits'. 86 | // Returns NULL in case of memory error. 87 | // Special case of VP8LAllocateHistogramSet, with size equals 1. 88 | VP8LHistogram* VP8LAllocateHistogram(int cache_bits); 89 | 90 | // Accumulate a token 'v' into a histogram. 91 | void VP8LHistogramAddSinglePixOrCopy(VP8LHistogram* const histo, 92 | const PixOrCopy* const v); 93 | 94 | // Estimate how many bits the combined entropy of literals and distance 95 | // approximately maps to. 96 | double VP8LHistogramEstimateBits(const VP8LHistogram* const p); 97 | 98 | // This function estimates the cost in bits excluding the bits needed to 99 | // represent the entropy code itself. 100 | double VP8LHistogramEstimateBitsBulk(const VP8LHistogram* const p); 101 | 102 | static WEBP_INLINE int VP8LHistogramNumCodes(int palette_code_bits) { 103 | return NUM_LITERAL_CODES + NUM_LENGTH_CODES + 104 | ((palette_code_bits > 0) ? (1 << palette_code_bits) : 0); 105 | } 106 | 107 | // Builds the histogram image. 108 | int VP8LGetHistoImageSymbols(int xsize, int ysize, 109 | const VP8LBackwardRefs* const refs, 110 | int quality, int histogram_bits, int cache_bits, 111 | VP8LHistogramSet* const image_in, 112 | uint16_t* const histogram_symbols); 113 | 114 | #ifdef __cplusplus 115 | } 116 | #endif 117 | 118 | #endif // WEBP_ENC_HISTOGRAM_H_ 119 | -------------------------------------------------------------------------------- /libwebp/src/enc/picture_psnr.c: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // WebPPicture tools for measuring distortion 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #include 15 | 16 | #include "./vp8enci.h" 17 | 18 | //------------------------------------------------------------------------------ 19 | // local-min distortion 20 | // 21 | // For every pixel in the *reference* picture, we search for the local best 22 | // match in the compressed image. This is not a symmetrical measure. 23 | 24 | #define RADIUS 2 // search radius. Shouldn't be too large. 25 | 26 | static float AccumulateLSIM(const uint8_t* src, int src_stride, 27 | const uint8_t* ref, int ref_stride, 28 | int w, int h) { 29 | int x, y; 30 | double total_sse = 0.; 31 | for (y = 0; y < h; ++y) { 32 | const int y_0 = (y - RADIUS < 0) ? 0 : y - RADIUS; 33 | const int y_1 = (y + RADIUS + 1 >= h) ? h : y + RADIUS + 1; 34 | for (x = 0; x < w; ++x) { 35 | const int x_0 = (x - RADIUS < 0) ? 0 : x - RADIUS; 36 | const int x_1 = (x + RADIUS + 1 >= w) ? w : x + RADIUS + 1; 37 | double best_sse = 255. * 255.; 38 | const double value = (double)ref[y * ref_stride + x]; 39 | int i, j; 40 | for (j = y_0; j < y_1; ++j) { 41 | const uint8_t* s = src + j * src_stride; 42 | for (i = x_0; i < x_1; ++i) { 43 | const double sse = (double)(s[i] - value) * (s[i] - value); 44 | if (sse < best_sse) best_sse = sse; 45 | } 46 | } 47 | total_sse += best_sse; 48 | } 49 | } 50 | return (float)total_sse; 51 | } 52 | #undef RADIUS 53 | 54 | //------------------------------------------------------------------------------ 55 | // Distortion 56 | 57 | // Max value returned in case of exact similarity. 58 | static const double kMinDistortion_dB = 99.; 59 | static float GetPSNR(const double v) { 60 | return (float)((v > 0.) ? -4.3429448 * log(v / (255 * 255.)) 61 | : kMinDistortion_dB); 62 | } 63 | 64 | int WebPPictureDistortion(const WebPPicture* src, const WebPPicture* ref, 65 | int type, float result[5]) { 66 | DistoStats stats[5]; 67 | int has_alpha; 68 | int uv_w, uv_h; 69 | 70 | if (src == NULL || ref == NULL || 71 | src->width != ref->width || src->height != ref->height || 72 | src->y == NULL || ref->y == NULL || 73 | src->u == NULL || ref->u == NULL || 74 | src->v == NULL || ref->v == NULL || 75 | result == NULL) { 76 | return 0; 77 | } 78 | // TODO(skal): provide distortion for ARGB too. 79 | if (src->use_argb == 1 || src->use_argb != ref->use_argb) { 80 | return 0; 81 | } 82 | 83 | has_alpha = !!(src->colorspace & WEBP_CSP_ALPHA_BIT); 84 | if (has_alpha != !!(ref->colorspace & WEBP_CSP_ALPHA_BIT) || 85 | (has_alpha && (src->a == NULL || ref->a == NULL))) { 86 | return 0; 87 | } 88 | 89 | memset(stats, 0, sizeof(stats)); 90 | 91 | uv_w = (src->width + 1) >> 1; 92 | uv_h = (src->height + 1) >> 1; 93 | if (type >= 2) { 94 | float sse[4]; 95 | sse[0] = AccumulateLSIM(src->y, src->y_stride, 96 | ref->y, ref->y_stride, src->width, src->height); 97 | sse[1] = AccumulateLSIM(src->u, src->uv_stride, 98 | ref->u, ref->uv_stride, uv_w, uv_h); 99 | sse[2] = AccumulateLSIM(src->v, src->uv_stride, 100 | ref->v, ref->uv_stride, uv_w, uv_h); 101 | sse[3] = has_alpha ? AccumulateLSIM(src->a, src->a_stride, 102 | ref->a, ref->a_stride, 103 | src->width, src->height) 104 | : 0.f; 105 | result[0] = GetPSNR(sse[0] / (src->width * src->height)); 106 | result[1] = GetPSNR(sse[1] / (uv_w * uv_h)); 107 | result[2] = GetPSNR(sse[2] / (uv_w * uv_h)); 108 | result[3] = GetPSNR(sse[3] / (src->width * src->height)); 109 | { 110 | double total_sse = sse[0] + sse[1] + sse[2]; 111 | int total_pixels = src->width * src->height + 2 * uv_w * uv_h; 112 | if (has_alpha) { 113 | total_pixels += src->width * src->height; 114 | total_sse += sse[3]; 115 | } 116 | result[4] = GetPSNR(total_sse / total_pixels); 117 | } 118 | } else { 119 | int c; 120 | VP8SSIMAccumulatePlane(src->y, src->y_stride, 121 | ref->y, ref->y_stride, 122 | src->width, src->height, &stats[0]); 123 | VP8SSIMAccumulatePlane(src->u, src->uv_stride, 124 | ref->u, ref->uv_stride, 125 | uv_w, uv_h, &stats[1]); 126 | VP8SSIMAccumulatePlane(src->v, src->uv_stride, 127 | ref->v, ref->uv_stride, 128 | uv_w, uv_h, &stats[2]); 129 | if (has_alpha) { 130 | VP8SSIMAccumulatePlane(src->a, src->a_stride, 131 | ref->a, ref->a_stride, 132 | src->width, src->height, &stats[3]); 133 | } 134 | for (c = 0; c <= 4; ++c) { 135 | if (type == 1) { 136 | const double v = VP8SSIMGet(&stats[c]); 137 | result[c] = (float)((v < 1.) ? -10.0 * log10(1. - v) 138 | : kMinDistortion_dB); 139 | } else { 140 | const double v = VP8SSIMGetSquaredError(&stats[c]); 141 | result[c] = GetPSNR(v); 142 | } 143 | // Accumulate forward 144 | if (c < 4) VP8SSIMAddStats(&stats[c], &stats[4]); 145 | } 146 | } 147 | return 1; 148 | } 149 | 150 | //------------------------------------------------------------------------------ 151 | -------------------------------------------------------------------------------- /libwebp/src/enc/picture_tools.c: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // WebPPicture tools: alpha handling, etc. 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #include "./vp8enci.h" 15 | #include "../dsp/yuv.h" 16 | 17 | static WEBP_INLINE uint32_t MakeARGB32(int r, int g, int b) { 18 | return (0xff000000u | (r << 16) | (g << 8) | b); 19 | } 20 | 21 | //------------------------------------------------------------------------------ 22 | // Helper: clean up fully transparent area to help compressibility. 23 | 24 | #define SIZE 8 25 | #define SIZE2 (SIZE / 2) 26 | static int is_transparent_area(const uint8_t* ptr, int stride, int size) { 27 | int y, x; 28 | for (y = 0; y < size; ++y) { 29 | for (x = 0; x < size; ++x) { 30 | if (ptr[x]) { 31 | return 0; 32 | } 33 | } 34 | ptr += stride; 35 | } 36 | return 1; 37 | } 38 | 39 | static int is_transparent_argb_area(const uint32_t* ptr, int stride, int size) { 40 | int y, x; 41 | for (y = 0; y < size; ++y) { 42 | for (x = 0; x < size; ++x) { 43 | if (ptr[x] & 0xff000000u) { 44 | return 0; 45 | } 46 | } 47 | ptr += stride; 48 | } 49 | return 1; 50 | } 51 | 52 | static void flatten(uint8_t* ptr, int v, int stride, int size) { 53 | int y; 54 | for (y = 0; y < size; ++y) { 55 | memset(ptr, v, size); 56 | ptr += stride; 57 | } 58 | } 59 | 60 | static void flatten_argb(uint32_t* ptr, uint32_t v, int stride, int size) { 61 | int x, y; 62 | for (y = 0; y < size; ++y) { 63 | for (x = 0; x < size; ++x) ptr[x] = v; 64 | ptr += stride; 65 | } 66 | } 67 | 68 | void WebPCleanupTransparentArea(WebPPicture* pic) { 69 | int x, y, w, h; 70 | if (pic == NULL) return; 71 | w = pic->width / SIZE; 72 | h = pic->height / SIZE; 73 | 74 | // note: we ignore the left-overs on right/bottom 75 | if (pic->use_argb) { 76 | uint32_t argb_value = 0; 77 | for (y = 0; y < h; ++y) { 78 | int need_reset = 1; 79 | for (x = 0; x < w; ++x) { 80 | const int off = (y * pic->argb_stride + x) * SIZE; 81 | if (is_transparent_argb_area(pic->argb + off, pic->argb_stride, SIZE)) { 82 | if (need_reset) { 83 | argb_value = pic->argb[off]; 84 | need_reset = 0; 85 | } 86 | flatten_argb(pic->argb + off, argb_value, pic->argb_stride, SIZE); 87 | } else { 88 | need_reset = 1; 89 | } 90 | } 91 | } 92 | } else { 93 | const uint8_t* const a_ptr = pic->a; 94 | int values[3] = { 0 }; 95 | if (a_ptr == NULL) return; // nothing to do 96 | for (y = 0; y < h; ++y) { 97 | int need_reset = 1; 98 | for (x = 0; x < w; ++x) { 99 | const int off_a = (y * pic->a_stride + x) * SIZE; 100 | const int off_y = (y * pic->y_stride + x) * SIZE; 101 | const int off_uv = (y * pic->uv_stride + x) * SIZE2; 102 | if (is_transparent_area(a_ptr + off_a, pic->a_stride, SIZE)) { 103 | if (need_reset) { 104 | values[0] = pic->y[off_y]; 105 | values[1] = pic->u[off_uv]; 106 | values[2] = pic->v[off_uv]; 107 | need_reset = 0; 108 | } 109 | flatten(pic->y + off_y, values[0], pic->y_stride, SIZE); 110 | flatten(pic->u + off_uv, values[1], pic->uv_stride, SIZE2); 111 | flatten(pic->v + off_uv, values[2], pic->uv_stride, SIZE2); 112 | } else { 113 | need_reset = 1; 114 | } 115 | } 116 | } 117 | } 118 | } 119 | 120 | #undef SIZE 121 | #undef SIZE2 122 | 123 | //------------------------------------------------------------------------------ 124 | // Blend color and remove transparency info 125 | 126 | #define BLEND(V0, V1, ALPHA) \ 127 | ((((V0) * (255 - (ALPHA)) + (V1) * (ALPHA)) * 0x101) >> 16) 128 | #define BLEND_10BIT(V0, V1, ALPHA) \ 129 | ((((V0) * (1020 - (ALPHA)) + (V1) * (ALPHA)) * 0x101) >> 18) 130 | 131 | void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb) { 132 | const int red = (background_rgb >> 16) & 0xff; 133 | const int green = (background_rgb >> 8) & 0xff; 134 | const int blue = (background_rgb >> 0) & 0xff; 135 | int x, y; 136 | if (pic == NULL) return; 137 | if (!pic->use_argb) { 138 | const int uv_width = (pic->width >> 1); // omit last pixel during u/v loop 139 | const int Y0 = VP8RGBToY(red, green, blue, YUV_HALF); 140 | // VP8RGBToU/V expects the u/v values summed over four pixels 141 | const int U0 = VP8RGBToU(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF); 142 | const int V0 = VP8RGBToV(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF); 143 | const int has_alpha = pic->colorspace & WEBP_CSP_ALPHA_BIT; 144 | if (!has_alpha || pic->a == NULL) return; // nothing to do 145 | for (y = 0; y < pic->height; ++y) { 146 | // Luma blending 147 | uint8_t* const y_ptr = pic->y + y * pic->y_stride; 148 | uint8_t* const a_ptr = pic->a + y * pic->a_stride; 149 | for (x = 0; x < pic->width; ++x) { 150 | const int alpha = a_ptr[x]; 151 | if (alpha < 0xff) { 152 | y_ptr[x] = BLEND(Y0, y_ptr[x], a_ptr[x]); 153 | } 154 | } 155 | // Chroma blending every even line 156 | if ((y & 1) == 0) { 157 | uint8_t* const u = pic->u + (y >> 1) * pic->uv_stride; 158 | uint8_t* const v = pic->v + (y >> 1) * pic->uv_stride; 159 | uint8_t* const a_ptr2 = 160 | (y + 1 == pic->height) ? a_ptr : a_ptr + pic->a_stride; 161 | for (x = 0; x < uv_width; ++x) { 162 | // Average four alpha values into a single blending weight. 163 | // TODO(skal): might lead to visible contouring. Can we do better? 164 | const int alpha = 165 | a_ptr[2 * x + 0] + a_ptr[2 * x + 1] + 166 | a_ptr2[2 * x + 0] + a_ptr2[2 * x + 1]; 167 | u[x] = BLEND_10BIT(U0, u[x], alpha); 168 | v[x] = BLEND_10BIT(V0, v[x], alpha); 169 | } 170 | if (pic->width & 1) { // rightmost pixel 171 | const int alpha = 2 * (a_ptr[2 * x + 0] + a_ptr2[2 * x + 0]); 172 | u[x] = BLEND_10BIT(U0, u[x], alpha); 173 | v[x] = BLEND_10BIT(V0, v[x], alpha); 174 | } 175 | } 176 | memset(a_ptr, 0xff, pic->width); 177 | } 178 | } else { 179 | uint32_t* argb = pic->argb; 180 | const uint32_t background = MakeARGB32(red, green, blue); 181 | for (y = 0; y < pic->height; ++y) { 182 | for (x = 0; x < pic->width; ++x) { 183 | const int alpha = (argb[x] >> 24) & 0xff; 184 | if (alpha != 0xff) { 185 | if (alpha > 0) { 186 | int r = (argb[x] >> 16) & 0xff; 187 | int g = (argb[x] >> 8) & 0xff; 188 | int b = (argb[x] >> 0) & 0xff; 189 | r = BLEND(red, r, alpha); 190 | g = BLEND(green, g, alpha); 191 | b = BLEND(blue, b, alpha); 192 | argb[x] = MakeARGB32(r, g, b); 193 | } else { 194 | argb[x] = background; 195 | } 196 | } 197 | } 198 | argb += pic->argb_stride; 199 | } 200 | } 201 | } 202 | 203 | #undef BLEND 204 | #undef BLEND_10BIT 205 | 206 | //------------------------------------------------------------------------------ 207 | -------------------------------------------------------------------------------- /libwebp/src/enc/vp8li.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Lossless encoder: internal header. 11 | // 12 | // Author: Vikas Arora (vikaas.arora@gmail.com) 13 | 14 | #ifndef WEBP_ENC_VP8LI_H_ 15 | #define WEBP_ENC_VP8LI_H_ 16 | 17 | #include "./backward_references.h" 18 | #include "./histogram.h" 19 | #include "../utils/bit_writer.h" 20 | #include "../webp/encode.h" 21 | #include "../webp/format_constants.h" 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | typedef struct { 28 | const WebPConfig* config_; // user configuration and parameters 29 | const WebPPicture* pic_; // input picture. 30 | 31 | uint32_t* argb_; // Transformed argb image data. 32 | uint32_t* argb_scratch_; // Scratch memory for argb rows 33 | // (used for prediction). 34 | uint32_t* transform_data_; // Scratch memory for transform data. 35 | int current_width_; // Corresponds to packed image width. 36 | 37 | // Encoding parameters derived from quality parameter. 38 | int histo_bits_; 39 | int transform_bits_; 40 | int cache_bits_; // If equal to 0, don't use color cache. 41 | 42 | // Encoding parameters derived from image characteristics. 43 | int use_cross_color_; 44 | int use_subtract_green_; 45 | int use_predict_; 46 | int use_palette_; 47 | int palette_size_; 48 | uint32_t palette_[MAX_PALETTE_SIZE]; 49 | 50 | // Some 'scratch' (potentially large) objects. 51 | struct VP8LBackwardRefs refs_[2]; // Backward Refs array corresponding to 52 | // LZ77 & RLE coding. 53 | VP8LHashChain hash_chain_; // HashChain data for constructing 54 | // backward references. 55 | } VP8LEncoder; 56 | 57 | //------------------------------------------------------------------------------ 58 | // internal functions. Not public. 59 | 60 | // Encodes the picture. 61 | // Returns 0 if config or picture is NULL or picture doesn't have valid argb 62 | // input. 63 | int VP8LEncodeImage(const WebPConfig* const config, 64 | const WebPPicture* const picture); 65 | 66 | // Encodes the main image stream using the supplied bit writer. 67 | WebPEncodingError VP8LEncodeStream(const WebPConfig* const config, 68 | const WebPPicture* const picture, 69 | VP8LBitWriter* const bw); 70 | 71 | //------------------------------------------------------------------------------ 72 | 73 | #ifdef __cplusplus 74 | } // extern "C" 75 | #endif 76 | 77 | #endif /* WEBP_ENC_VP8LI_H_ */ 78 | -------------------------------------------------------------------------------- /libwebp/src/libwebp.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: libwebp 7 | Description: Library for the WebP graphics format 8 | Version: @PACKAGE_VERSION@ 9 | Cflags: -I${includedir} 10 | Libs: -L${libdir} -lwebp 11 | Libs.private: -lm @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ 12 | -------------------------------------------------------------------------------- /libwebp/src/libwebpdecoder.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: libwebpdecoder 7 | Description: Library for the WebP graphics format (decode only) 8 | Version: @PACKAGE_VERSION@ 9 | Cflags: -I${includedir} 10 | Libs: -L${libdir} -lwebpdecoder 11 | Libs.private: -lm @PTHREAD_CFLAGS@ @PTHREAD_LIBS@ 12 | -------------------------------------------------------------------------------- /libwebp/src/mux/Makefile.am: -------------------------------------------------------------------------------- 1 | lib_LTLIBRARIES = libwebpmux.la 2 | 3 | libwebpmux_la_SOURCES = 4 | libwebpmux_la_SOURCES += muxedit.c 5 | libwebpmux_la_SOURCES += muxi.h 6 | libwebpmux_la_SOURCES += muxinternal.c 7 | libwebpmux_la_SOURCES += muxread.c 8 | 9 | libwebpmuxinclude_HEADERS = 10 | libwebpmuxinclude_HEADERS += ../webp/mux.h 11 | libwebpmuxinclude_HEADERS += ../webp/mux_types.h 12 | libwebpmuxinclude_HEADERS += ../webp/types.h 13 | 14 | libwebpmux_la_LIBADD = ../libwebp.la 15 | libwebpmux_la_LDFLAGS = -no-undefined -version-info 1:2:0 16 | libwebpmuxincludedir = $(includedir)/webp 17 | pkgconfig_DATA = libwebpmux.pc 18 | -------------------------------------------------------------------------------- /libwebp/src/mux/libwebpmux.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: libwebpmux 7 | Description: Library for manipulating the WebP graphics format container 8 | Version: @PACKAGE_VERSION@ 9 | Requires: libwebp >= 0.2.0 10 | Cflags: -I${includedir} 11 | Libs: -L${libdir} -lwebpmux 12 | -------------------------------------------------------------------------------- /libwebp/src/utils/Makefile.am: -------------------------------------------------------------------------------- 1 | noinst_LTLIBRARIES = libwebputils.la 2 | 3 | if BUILD_LIBWEBPDECODER 4 | noinst_LTLIBRARIES += libwebputilsdecode.la 5 | endif 6 | 7 | common_HEADERS = ../webp/types.h 8 | commondir = $(includedir)/webp 9 | 10 | COMMON_SOURCES = 11 | COMMON_SOURCES += bit_reader.c 12 | COMMON_SOURCES += bit_reader.h 13 | COMMON_SOURCES += bit_reader_inl.h 14 | COMMON_SOURCES += color_cache.c 15 | COMMON_SOURCES += color_cache.h 16 | COMMON_SOURCES += endian_inl.h 17 | COMMON_SOURCES += filters.c 18 | COMMON_SOURCES += filters.h 19 | COMMON_SOURCES += huffman.c 20 | COMMON_SOURCES += huffman.h 21 | COMMON_SOURCES += quant_levels_dec.c 22 | COMMON_SOURCES += quant_levels_dec.h 23 | COMMON_SOURCES += rescaler.c 24 | COMMON_SOURCES += rescaler.h 25 | COMMON_SOURCES += random.c 26 | COMMON_SOURCES += random.h 27 | COMMON_SOURCES += thread.c 28 | COMMON_SOURCES += thread.h 29 | COMMON_SOURCES += utils.c 30 | COMMON_SOURCES += utils.h 31 | 32 | ENC_SOURCES = 33 | ENC_SOURCES += bit_writer.c 34 | ENC_SOURCES += bit_writer.h 35 | ENC_SOURCES += huffman_encode.c 36 | ENC_SOURCES += huffman_encode.h 37 | ENC_SOURCES += quant_levels.c 38 | ENC_SOURCES += quant_levels.h 39 | 40 | libwebputils_la_SOURCES = $(COMMON_SOURCES) $(ENC_SOURCES) 41 | 42 | if BUILD_LIBWEBPDECODER 43 | libwebputilsdecode_la_SOURCES = $(COMMON_SOURCES) 44 | endif 45 | -------------------------------------------------------------------------------- /libwebp/src/utils/bit_reader.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Boolean decoder non-inlined methods 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #ifdef HAVE_CONFIG_H 15 | #include "../webp/config.h" 16 | #endif 17 | 18 | #include "./bit_reader_inl.h" 19 | 20 | //------------------------------------------------------------------------------ 21 | // VP8BitReader 22 | 23 | void VP8InitBitReader(VP8BitReader* const br, 24 | const uint8_t* const start, const uint8_t* const end) { 25 | assert(br != NULL); 26 | assert(start != NULL); 27 | assert(start <= end); 28 | br->range_ = 255 - 1; 29 | br->buf_ = start; 30 | br->buf_end_ = end; 31 | br->value_ = 0; 32 | br->bits_ = -8; // to load the very first 8bits 33 | br->eof_ = 0; 34 | VP8LoadNewBytes(br); 35 | } 36 | 37 | void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset) { 38 | if (br->buf_ != NULL) { 39 | br->buf_ += offset; 40 | br->buf_end_ += offset; 41 | } 42 | } 43 | 44 | const uint8_t kVP8Log2Range[128] = { 45 | 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 46 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 47 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 48 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 49 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 50 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 51 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 52 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 53 | 0 54 | }; 55 | 56 | // range = ((range - 1) << kVP8Log2Range[range]) + 1 57 | const range_t kVP8NewRange[128] = { 58 | 127, 127, 191, 127, 159, 191, 223, 127, 59 | 143, 159, 175, 191, 207, 223, 239, 127, 60 | 135, 143, 151, 159, 167, 175, 183, 191, 61 | 199, 207, 215, 223, 231, 239, 247, 127, 62 | 131, 135, 139, 143, 147, 151, 155, 159, 63 | 163, 167, 171, 175, 179, 183, 187, 191, 64 | 195, 199, 203, 207, 211, 215, 219, 223, 65 | 227, 231, 235, 239, 243, 247, 251, 127, 66 | 129, 131, 133, 135, 137, 139, 141, 143, 67 | 145, 147, 149, 151, 153, 155, 157, 159, 68 | 161, 163, 165, 167, 169, 171, 173, 175, 69 | 177, 179, 181, 183, 185, 187, 189, 191, 70 | 193, 195, 197, 199, 201, 203, 205, 207, 71 | 209, 211, 213, 215, 217, 219, 221, 223, 72 | 225, 227, 229, 231, 233, 235, 237, 239, 73 | 241, 243, 245, 247, 249, 251, 253, 127 74 | }; 75 | 76 | void VP8LoadFinalBytes(VP8BitReader* const br) { 77 | assert(br != NULL && br->buf_ != NULL); 78 | // Only read 8bits at a time 79 | if (br->buf_ < br->buf_end_) { 80 | br->bits_ += 8; 81 | br->value_ = (bit_t)(*br->buf_++) | (br->value_ << 8); 82 | } else if (!br->eof_) { 83 | br->value_ <<= 8; 84 | br->bits_ += 8; 85 | br->eof_ = 1; 86 | } 87 | } 88 | 89 | //------------------------------------------------------------------------------ 90 | // Higher-level calls 91 | 92 | uint32_t VP8GetValue(VP8BitReader* const br, int bits) { 93 | uint32_t v = 0; 94 | while (bits-- > 0) { 95 | v |= VP8GetBit(br, 0x80) << bits; 96 | } 97 | return v; 98 | } 99 | 100 | int32_t VP8GetSignedValue(VP8BitReader* const br, int bits) { 101 | const int value = VP8GetValue(br, bits); 102 | return VP8Get(br) ? -value : value; 103 | } 104 | 105 | //------------------------------------------------------------------------------ 106 | // VP8LBitReader 107 | 108 | #define VP8L_LOG8_WBITS 4 // Number of bytes needed to store VP8L_WBITS bits. 109 | 110 | #if !defined(WEBP_FORCE_ALIGNED) && \ 111 | (defined(__arm__) || defined(_M_ARM) || defined(__aarch64__) || \ 112 | defined(__i386__) || defined(_M_IX86) || \ 113 | defined(__x86_64__) || defined(_M_X64)) 114 | #define VP8L_USE_UNALIGNED_LOAD 115 | #endif 116 | 117 | static const uint32_t kBitMask[VP8L_MAX_NUM_BIT_READ + 1] = { 118 | 0, 119 | 0x000001, 0x000003, 0x000007, 0x00000f, 120 | 0x00001f, 0x00003f, 0x00007f, 0x0000ff, 121 | 0x0001ff, 0x0003ff, 0x0007ff, 0x000fff, 122 | 0x001fff, 0x003fff, 0x007fff, 0x00ffff, 123 | 0x01ffff, 0x03ffff, 0x07ffff, 0x0fffff, 124 | 0x1fffff, 0x3fffff, 0x7fffff, 0xffffff 125 | }; 126 | 127 | void VP8LInitBitReader(VP8LBitReader* const br, const uint8_t* const start, 128 | size_t length) { 129 | size_t i; 130 | vp8l_val_t value = 0; 131 | assert(br != NULL); 132 | assert(start != NULL); 133 | assert(length < 0xfffffff8u); // can't happen with a RIFF chunk. 134 | 135 | br->len_ = length; 136 | br->val_ = 0; 137 | br->bit_pos_ = 0; 138 | br->eos_ = 0; 139 | br->error_ = 0; 140 | 141 | if (length > sizeof(br->val_)) { 142 | length = sizeof(br->val_); 143 | } 144 | for (i = 0; i < length; ++i) { 145 | value |= (vp8l_val_t)start[i] << (8 * i); 146 | } 147 | br->val_ = value; 148 | br->pos_ = length; 149 | br->buf_ = start; 150 | } 151 | 152 | void VP8LBitReaderSetBuffer(VP8LBitReader* const br, 153 | const uint8_t* const buf, size_t len) { 154 | assert(br != NULL); 155 | assert(buf != NULL); 156 | assert(len < 0xfffffff8u); // can't happen with a RIFF chunk. 157 | br->buf_ = buf; 158 | br->len_ = len; 159 | // pos_ > len_ should be considered a param error. 160 | br->error_ = (br->pos_ > br->len_); 161 | br->eos_ = br->error_ || VP8LIsEndOfStream(br); 162 | } 163 | 164 | // If not at EOS, reload up to VP8L_LBITS byte-by-byte 165 | static void ShiftBytes(VP8LBitReader* const br) { 166 | while (br->bit_pos_ >= 8 && br->pos_ < br->len_) { 167 | br->val_ >>= 8; 168 | br->val_ |= ((vp8l_val_t)br->buf_[br->pos_]) << (VP8L_LBITS - 8); 169 | ++br->pos_; 170 | br->bit_pos_ -= 8; 171 | } 172 | br->eos_ = VP8LIsEndOfStream(br); 173 | } 174 | 175 | void VP8LDoFillBitWindow(VP8LBitReader* const br) { 176 | assert(br->bit_pos_ >= VP8L_WBITS); 177 | // TODO(jzern): given the fixed read size it may be possible to force 178 | // alignment in this block. 179 | #if defined(VP8L_USE_UNALIGNED_LOAD) 180 | if (br->pos_ + sizeof(br->val_) < br->len_) { 181 | br->val_ >>= VP8L_WBITS; 182 | br->bit_pos_ -= VP8L_WBITS; 183 | // The expression below needs a little-endian arch to work correctly. 184 | // This gives a large speedup for decoding speed. 185 | br->val_ |= (vp8l_val_t)*(const uint32_t*)(br->buf_ + br->pos_) << 186 | (VP8L_LBITS - VP8L_WBITS); 187 | br->pos_ += VP8L_LOG8_WBITS; 188 | return; 189 | } 190 | #endif 191 | ShiftBytes(br); // Slow path. 192 | } 193 | 194 | uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits) { 195 | assert(n_bits >= 0); 196 | // Flag an error if end_of_stream or n_bits is more than allowed limit. 197 | if (!br->eos_ && n_bits <= VP8L_MAX_NUM_BIT_READ) { 198 | const uint32_t val = 199 | (uint32_t)(br->val_ >> br->bit_pos_) & kBitMask[n_bits]; 200 | const int new_bits = br->bit_pos_ + n_bits; 201 | br->bit_pos_ = new_bits; 202 | ShiftBytes(br); 203 | return val; 204 | } else { 205 | br->error_ = 1; 206 | return 0; 207 | } 208 | } 209 | 210 | //------------------------------------------------------------------------------ 211 | -------------------------------------------------------------------------------- /libwebp/src/utils/bit_reader.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Boolean decoder 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | // Vikas Arora (vikaas.arora@gmail.com) 14 | 15 | #ifndef WEBP_UTILS_BIT_READER_H_ 16 | #define WEBP_UTILS_BIT_READER_H_ 17 | 18 | #include 19 | #ifdef _MSC_VER 20 | #include // _byteswap_ulong 21 | #endif 22 | #include "../webp/types.h" 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | // The Boolean decoder needs to maintain infinite precision on the value_ field. 29 | // However, since range_ is only 8bit, we only need an active window of 8 bits 30 | // for value_. Left bits (MSB) gets zeroed and shifted away when value_ falls 31 | // below 128, range_ is updated, and fresh bits read from the bitstream are 32 | // brought in as LSB. To avoid reading the fresh bits one by one (slow), we 33 | // cache BITS of them ahead. The total of (BITS + 8) bits must fit into a 34 | // natural register (with type bit_t). To fetch BITS bits from bitstream we 35 | // use a type lbit_t. 36 | // 37 | // BITS can be any multiple of 8 from 8 to 56 (inclusive). 38 | // Pick values that fit natural register size. 39 | 40 | #if defined(__i386__) || defined(_M_IX86) // x86 32bit 41 | #define BITS 24 42 | #elif defined(__x86_64__) || defined(_M_X64) // x86 64bit 43 | #define BITS 56 44 | #elif defined(__arm__) || defined(_M_ARM) // ARM 45 | #define BITS 24 46 | #elif defined(__mips__) // MIPS 47 | #define BITS 24 48 | #else // reasonable default 49 | #define BITS 24 // TODO(skal): test aarch64 and find the proper BITS value. 50 | #endif 51 | 52 | //------------------------------------------------------------------------------ 53 | // Derived types and constants: 54 | // bit_t = natural register type for storing 'value_' (which is BITS+8 bits) 55 | // range_t = register for 'range_' (which is 8bits only) 56 | 57 | #if (BITS > 24) 58 | typedef uint64_t bit_t; 59 | #else 60 | typedef uint32_t bit_t; 61 | #endif 62 | 63 | typedef uint32_t range_t; 64 | 65 | //------------------------------------------------------------------------------ 66 | // Bitreader 67 | 68 | typedef struct VP8BitReader VP8BitReader; 69 | struct VP8BitReader { 70 | // boolean decoder (keep the field ordering as is!) 71 | bit_t value_; // current value 72 | range_t range_; // current range minus 1. In [127, 254] interval. 73 | int bits_; // number of valid bits left 74 | // read buffer 75 | const uint8_t* buf_; // next byte to be read 76 | const uint8_t* buf_end_; // end of read buffer 77 | int eof_; // true if input is exhausted 78 | }; 79 | 80 | // Initialize the bit reader and the boolean decoder. 81 | void VP8InitBitReader(VP8BitReader* const br, 82 | const uint8_t* const start, const uint8_t* const end); 83 | 84 | // Update internal pointers to displace the byte buffer by the 85 | // relative offset 'offset'. 86 | void VP8RemapBitReader(VP8BitReader* const br, ptrdiff_t offset); 87 | 88 | // return the next value made of 'num_bits' bits 89 | uint32_t VP8GetValue(VP8BitReader* const br, int num_bits); 90 | static WEBP_INLINE uint32_t VP8Get(VP8BitReader* const br) { 91 | return VP8GetValue(br, 1); 92 | } 93 | 94 | // return the next value with sign-extension. 95 | int32_t VP8GetSignedValue(VP8BitReader* const br, int num_bits); 96 | 97 | // bit_reader_inl.h will implement the following methods: 98 | // static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) 99 | // static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) 100 | // and should be included by the .c files that actually need them. 101 | // This is to avoid recompiling the whole library whenever this file is touched, 102 | // and also allowing platform-specific ad-hoc hacks. 103 | 104 | // ----------------------------------------------------------------------------- 105 | // Bitreader for lossless format 106 | 107 | // maximum number of bits (inclusive) the bit-reader can handle: 108 | #define VP8L_MAX_NUM_BIT_READ 24 109 | 110 | #define VP8L_LBITS 64 // Number of bits prefetched. 111 | #define VP8L_WBITS 32 // Minimum number of bytes ready after VP8LFillBitWindow. 112 | 113 | typedef uint64_t vp8l_val_t; // right now, this bit-reader can only use 64bit. 114 | 115 | typedef struct { 116 | vp8l_val_t val_; // pre-fetched bits 117 | const uint8_t* buf_; // input byte buffer 118 | size_t len_; // buffer length 119 | size_t pos_; // byte position in buf_ 120 | int bit_pos_; // current bit-reading position in val_ 121 | int eos_; // bitstream is finished 122 | int error_; // an error occurred (buffer overflow attempt...) 123 | } VP8LBitReader; 124 | 125 | void VP8LInitBitReader(VP8LBitReader* const br, 126 | const uint8_t* const start, 127 | size_t length); 128 | 129 | // Sets a new data buffer. 130 | void VP8LBitReaderSetBuffer(VP8LBitReader* const br, 131 | const uint8_t* const buffer, size_t length); 132 | 133 | // Reads the specified number of bits from read buffer. 134 | // Flags an error in case end_of_stream or n_bits is more than the allowed limit 135 | // of VP8L_MAX_NUM_BIT_READ (inclusive). 136 | // Flags eos_ if this read attempt is going to cross the read buffer. 137 | uint32_t VP8LReadBits(VP8LBitReader* const br, int n_bits); 138 | 139 | // Return the prefetched bits, so they can be looked up. 140 | static WEBP_INLINE uint32_t VP8LPrefetchBits(VP8LBitReader* const br) { 141 | return (uint32_t)(br->val_ >> br->bit_pos_); 142 | } 143 | 144 | // Returns true if there was an attempt at reading bit past the end of 145 | // the buffer. Doesn't set br->eos_ flag. 146 | static WEBP_INLINE int VP8LIsEndOfStream(const VP8LBitReader* const br) { 147 | assert(br->pos_ <= br->len_); 148 | return (br->pos_ == br->len_) && (br->bit_pos_ > VP8L_LBITS); 149 | } 150 | 151 | // For jumping over a number of bits in the bit stream when accessed with 152 | // VP8LPrefetchBits and VP8LFillBitWindow. 153 | static WEBP_INLINE void VP8LSetBitPos(VP8LBitReader* const br, int val) { 154 | br->bit_pos_ = val; 155 | br->eos_ = VP8LIsEndOfStream(br); 156 | } 157 | 158 | // Advances the read buffer by 4 bytes to make room for reading next 32 bits. 159 | // Speed critical, but infrequent part of the code can be non-inlined. 160 | extern void VP8LDoFillBitWindow(VP8LBitReader* const br); 161 | static WEBP_INLINE void VP8LFillBitWindow(VP8LBitReader* const br) { 162 | if (br->bit_pos_ >= VP8L_WBITS) VP8LDoFillBitWindow(br); 163 | } 164 | 165 | #ifdef __cplusplus 166 | } // extern "C" 167 | #endif 168 | 169 | #endif /* WEBP_UTILS_BIT_READER_H_ */ 170 | -------------------------------------------------------------------------------- /libwebp/src/utils/bit_reader_inl.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Specific inlined methods for boolean decoder [VP8GetBit() ...] 11 | // This file should be included by the .c sources that actually need to call 12 | // these methods. 13 | // 14 | // Author: Skal (pascal.massimino@gmail.com) 15 | 16 | #ifndef WEBP_UTILS_BIT_READER_INL_H_ 17 | #define WEBP_UTILS_BIT_READER_INL_H_ 18 | 19 | #ifdef HAVE_CONFIG_H 20 | #include "../webp/config.h" 21 | #endif 22 | 23 | #ifdef WEBP_FORCE_ALIGNED 24 | #include // memcpy 25 | #endif 26 | 27 | #include "../dsp/dsp.h" 28 | #include "./bit_reader.h" 29 | #include "./endian_inl.h" 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | //------------------------------------------------------------------------------ 36 | // Derived type lbit_t = natural type for memory I/O 37 | 38 | #if (BITS > 32) 39 | typedef uint64_t lbit_t; 40 | #elif (BITS > 16) 41 | typedef uint32_t lbit_t; 42 | #elif (BITS > 8) 43 | typedef uint16_t lbit_t; 44 | #else 45 | typedef uint8_t lbit_t; 46 | #endif 47 | 48 | extern const uint8_t kVP8Log2Range[128]; 49 | extern const range_t kVP8NewRange[128]; 50 | 51 | // special case for the tail byte-reading 52 | void VP8LoadFinalBytes(VP8BitReader* const br); 53 | 54 | //------------------------------------------------------------------------------ 55 | // Inlined critical functions 56 | 57 | // makes sure br->value_ has at least BITS bits worth of data 58 | static WEBP_INLINE void VP8LoadNewBytes(VP8BitReader* const br) { 59 | assert(br != NULL && br->buf_ != NULL); 60 | // Read 'BITS' bits at a time if possible. 61 | if (br->buf_ + sizeof(lbit_t) <= br->buf_end_) { 62 | // convert memory type to register type (with some zero'ing!) 63 | bit_t bits; 64 | #if defined(WEBP_FORCE_ALIGNED) 65 | lbit_t in_bits; 66 | memcpy(&in_bits, br->buf_, sizeof(in_bits)); 67 | #elif defined(WEBP_USE_MIPS32) 68 | // This is needed because of un-aligned read. 69 | lbit_t in_bits; 70 | lbit_t* p_buf_ = (lbit_t*)br->buf_; 71 | __asm__ volatile( 72 | ".set push \n\t" 73 | ".set at \n\t" 74 | ".set macro \n\t" 75 | "ulw %[in_bits], 0(%[p_buf_]) \n\t" 76 | ".set pop \n\t" 77 | : [in_bits]"=r"(in_bits) 78 | : [p_buf_]"r"(p_buf_) 79 | : "memory", "at" 80 | ); 81 | #else 82 | const lbit_t in_bits = *(const lbit_t*)br->buf_; 83 | #endif 84 | br->buf_ += BITS >> 3; 85 | #if !defined(WORDS_BIGENDIAN) 86 | #if (BITS > 32) 87 | bits = BSwap64(in_bits); 88 | bits >>= 64 - BITS; 89 | #elif (BITS >= 24) 90 | bits = BSwap32(in_bits); 91 | bits >>= (32 - BITS); 92 | #elif (BITS == 16) 93 | bits = BSwap16(in_bits); 94 | #else // BITS == 8 95 | bits = (bit_t)in_bits; 96 | #endif // BITS > 32 97 | #else // WORDS_BIGENDIAN 98 | bits = (bit_t)in_bits; 99 | if (BITS != 8 * sizeof(bit_t)) bits >>= (8 * sizeof(bit_t) - BITS); 100 | #endif 101 | br->value_ = bits | (br->value_ << BITS); 102 | br->bits_ += BITS; 103 | } else { 104 | VP8LoadFinalBytes(br); // no need to be inlined 105 | } 106 | } 107 | 108 | // Read a bit with proba 'prob'. Speed-critical function! 109 | static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) { 110 | // Don't move this declaration! It makes a big speed difference to store 111 | // 'range' *before* calling VP8LoadNewBytes(), even if this function doesn't 112 | // alter br->range_ value. 113 | range_t range = br->range_; 114 | if (br->bits_ < 0) { 115 | VP8LoadNewBytes(br); 116 | } 117 | { 118 | const int pos = br->bits_; 119 | const range_t split = (range * prob) >> 8; 120 | const range_t value = (range_t)(br->value_ >> pos); 121 | #if defined(__arm__) || defined(_M_ARM) // ARM-specific 122 | const int bit = ((int)(split - value) >> 31) & 1; 123 | if (value > split) { 124 | range -= split + 1; 125 | br->value_ -= (bit_t)(split + 1) << pos; 126 | } else { 127 | range = split; 128 | } 129 | #else // faster version on x86 130 | int bit; // Don't use 'const int bit = (value > split);", it's slower. 131 | if (value > split) { 132 | range -= split + 1; 133 | br->value_ -= (bit_t)(split + 1) << pos; 134 | bit = 1; 135 | } else { 136 | range = split; 137 | bit = 0; 138 | } 139 | #endif 140 | if (range <= (range_t)0x7e) { 141 | const int shift = kVP8Log2Range[range]; 142 | range = kVP8NewRange[range]; 143 | br->bits_ -= shift; 144 | } 145 | br->range_ = range; 146 | return bit; 147 | } 148 | } 149 | 150 | // simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here) 151 | static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) { 152 | if (br->bits_ < 0) { 153 | VP8LoadNewBytes(br); 154 | } 155 | { 156 | const int pos = br->bits_; 157 | const range_t split = br->range_ >> 1; 158 | const range_t value = (range_t)(br->value_ >> pos); 159 | const int32_t mask = (int32_t)(split - value) >> 31; // -1 or 0 160 | br->bits_ -= 1; 161 | br->range_ += mask; 162 | br->range_ |= 1; 163 | br->value_ -= (bit_t)((split + 1) & mask) << pos; 164 | return (v ^ mask) - mask; 165 | } 166 | } 167 | 168 | #ifdef __cplusplus 169 | } // extern "C" 170 | #endif 171 | 172 | #endif // WEBP_UTILS_BIT_READER_INL_H_ 173 | -------------------------------------------------------------------------------- /libwebp/src/utils/bit_writer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Bit writing and boolean coder 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #ifndef WEBP_UTILS_BIT_WRITER_H_ 15 | #define WEBP_UTILS_BIT_WRITER_H_ 16 | 17 | #include "../webp/types.h" 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | //------------------------------------------------------------------------------ 24 | // Bit-writing 25 | 26 | typedef struct VP8BitWriter VP8BitWriter; 27 | struct VP8BitWriter { 28 | int32_t range_; // range-1 29 | int32_t value_; 30 | int run_; // number of outstanding bits 31 | int nb_bits_; // number of pending bits 32 | uint8_t* buf_; // internal buffer. Re-allocated regularly. Not owned. 33 | size_t pos_; 34 | size_t max_pos_; 35 | int error_; // true in case of error 36 | }; 37 | 38 | // Initialize the object. Allocates some initial memory based on expected_size. 39 | int VP8BitWriterInit(VP8BitWriter* const bw, size_t expected_size); 40 | // Finalize the bitstream coding. Returns a pointer to the internal buffer. 41 | uint8_t* VP8BitWriterFinish(VP8BitWriter* const bw); 42 | // Release any pending memory and zeroes the object. Not a mandatory call. 43 | // Only useful in case of error, when the internal buffer hasn't been grabbed! 44 | void VP8BitWriterWipeOut(VP8BitWriter* const bw); 45 | 46 | int VP8PutBit(VP8BitWriter* const bw, int bit, int prob); 47 | int VP8PutBitUniform(VP8BitWriter* const bw, int bit); 48 | void VP8PutValue(VP8BitWriter* const bw, int value, int nb_bits); 49 | void VP8PutSignedValue(VP8BitWriter* const bw, int value, int nb_bits); 50 | 51 | // Appends some bytes to the internal buffer. Data is copied. 52 | int VP8BitWriterAppend(VP8BitWriter* const bw, 53 | const uint8_t* data, size_t size); 54 | 55 | // return approximate write position (in bits) 56 | static WEBP_INLINE uint64_t VP8BitWriterPos(const VP8BitWriter* const bw) { 57 | return (uint64_t)(bw->pos_ + bw->run_) * 8 + 8 + bw->nb_bits_; 58 | } 59 | 60 | // Returns a pointer to the internal buffer. 61 | static WEBP_INLINE uint8_t* VP8BitWriterBuf(const VP8BitWriter* const bw) { 62 | return bw->buf_; 63 | } 64 | // Returns the size of the internal buffer. 65 | static WEBP_INLINE size_t VP8BitWriterSize(const VP8BitWriter* const bw) { 66 | return bw->pos_; 67 | } 68 | 69 | //------------------------------------------------------------------------------ 70 | // VP8LBitWriter 71 | 72 | #if defined(__x86_64__) || defined(_M_X64) // 64bit 73 | typedef uint64_t vp8l_atype_t; // accumulator type 74 | typedef uint32_t vp8l_wtype_t; // writing type 75 | #define WSWAP HToLE32 76 | #else 77 | typedef uint32_t vp8l_atype_t; 78 | typedef uint16_t vp8l_wtype_t; 79 | #define WSWAP HToLE16 80 | #endif 81 | 82 | typedef struct { 83 | vp8l_atype_t bits_; // bit accumulator 84 | int used_; // number of bits used in accumulator 85 | uint8_t* buf_; // start of buffer 86 | uint8_t* cur_; // current write position 87 | uint8_t* end_; // end of buffer 88 | 89 | // After all bits are written (VP8LBitWriterFinish()), the caller must observe 90 | // the state of error_. A value of 1 indicates that a memory allocation 91 | // failure has happened during bit writing. A value of 0 indicates successful 92 | // writing of bits. 93 | int error_; 94 | } VP8LBitWriter; 95 | 96 | static WEBP_INLINE size_t VP8LBitWriterNumBytes(VP8LBitWriter* const bw) { 97 | return (bw->cur_ - bw->buf_) + ((bw->used_ + 7) >> 3); 98 | } 99 | 100 | uint8_t* VP8LBitWriterFinish(VP8LBitWriter* const bw); 101 | 102 | // Returns 0 in case of memory allocation error. 103 | int VP8LBitWriterInit(VP8LBitWriter* const bw, size_t expected_size); 104 | 105 | void VP8LBitWriterDestroy(VP8LBitWriter* const bw); 106 | 107 | // This function writes bits into bytes in increasing addresses (little endian), 108 | // and within a byte least-significant-bit first. 109 | // This function can write up to 32 bits in one go, but VP8LBitReader can only 110 | // read 24 bits max (VP8L_MAX_NUM_BIT_READ). 111 | // VP8LBitWriter's error_ flag is set in case of memory allocation error. 112 | void VP8LWriteBits(VP8LBitWriter* const bw, int n_bits, uint32_t bits); 113 | 114 | //------------------------------------------------------------------------------ 115 | 116 | #ifdef __cplusplus 117 | } // extern "C" 118 | #endif 119 | 120 | #endif /* WEBP_UTILS_BIT_WRITER_H_ */ 121 | -------------------------------------------------------------------------------- /libwebp/src/utils/color_cache.c: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Color Cache for WebP Lossless 11 | // 12 | // Author: Jyrki Alakuijala (jyrki@google.com) 13 | 14 | #include 15 | #include 16 | #include "./color_cache.h" 17 | #include "../utils/utils.h" 18 | 19 | //------------------------------------------------------------------------------ 20 | // VP8LColorCache. 21 | 22 | int VP8LColorCacheInit(VP8LColorCache* const cc, int hash_bits) { 23 | const int hash_size = 1 << hash_bits; 24 | assert(cc != NULL); 25 | assert(hash_bits > 0); 26 | cc->colors_ = (uint32_t*)WebPSafeCalloc((uint64_t)hash_size, 27 | sizeof(*cc->colors_)); 28 | if (cc->colors_ == NULL) return 0; 29 | cc->hash_shift_ = 32 - hash_bits; 30 | return 1; 31 | } 32 | 33 | void VP8LColorCacheClear(VP8LColorCache* const cc) { 34 | if (cc != NULL) { 35 | WebPSafeFree(cc->colors_); 36 | cc->colors_ = NULL; 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /libwebp/src/utils/color_cache.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Color Cache for WebP Lossless 11 | // 12 | // Authors: Jyrki Alakuijala (jyrki@google.com) 13 | // Urvang Joshi (urvang@google.com) 14 | 15 | #ifndef WEBP_UTILS_COLOR_CACHE_H_ 16 | #define WEBP_UTILS_COLOR_CACHE_H_ 17 | 18 | #include "../webp/types.h" 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | // Main color cache struct. 25 | typedef struct { 26 | uint32_t *colors_; // color entries 27 | int hash_shift_; // Hash shift: 32 - hash_bits. 28 | } VP8LColorCache; 29 | 30 | static const uint32_t kHashMul = 0x1e35a7bd; 31 | 32 | static WEBP_INLINE uint32_t VP8LColorCacheLookup( 33 | const VP8LColorCache* const cc, uint32_t key) { 34 | assert(key <= (~0U >> cc->hash_shift_)); 35 | return cc->colors_[key]; 36 | } 37 | 38 | static WEBP_INLINE void VP8LColorCacheInsert(const VP8LColorCache* const cc, 39 | uint32_t argb) { 40 | const uint32_t key = (kHashMul * argb) >> cc->hash_shift_; 41 | cc->colors_[key] = argb; 42 | } 43 | 44 | static WEBP_INLINE int VP8LColorCacheGetIndex(const VP8LColorCache* const cc, 45 | uint32_t argb) { 46 | return (kHashMul * argb) >> cc->hash_shift_; 47 | } 48 | 49 | static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc, 50 | uint32_t argb) { 51 | const uint32_t key = (kHashMul * argb) >> cc->hash_shift_; 52 | return cc->colors_[key] == argb; 53 | } 54 | 55 | //------------------------------------------------------------------------------ 56 | 57 | // Initializes the color cache with 'hash_bits' bits for the keys. 58 | // Returns false in case of memory error. 59 | int VP8LColorCacheInit(VP8LColorCache* const color_cache, int hash_bits); 60 | 61 | // Delete the memory associated to color cache. 62 | void VP8LColorCacheClear(VP8LColorCache* const color_cache); 63 | 64 | //------------------------------------------------------------------------------ 65 | 66 | #ifdef __cplusplus 67 | } 68 | #endif 69 | 70 | #endif // WEBP_UTILS_COLOR_CACHE_H_ 71 | -------------------------------------------------------------------------------- /libwebp/src/utils/endian_inl.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Endian related functions. 11 | 12 | #ifndef WEBP_UTILS_ENDIAN_INL_H_ 13 | #define WEBP_UTILS_ENDIAN_INL_H_ 14 | 15 | #ifdef HAVE_CONFIG_H 16 | #include "../webp/config.h" 17 | #endif 18 | 19 | #include "../dsp/dsp.h" 20 | #include "../webp/types.h" 21 | 22 | // some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__) 23 | #if !defined(WORDS_BIGENDIAN) && \ 24 | (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \ 25 | (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))) 26 | #define WORDS_BIGENDIAN 27 | #endif 28 | 29 | #if defined(WORDS_BIGENDIAN) 30 | #define HToLE32 BSwap32 31 | #define HToLE16 BSwap16 32 | #else 33 | #define HToLE32(x) (x) 34 | #define HToLE16(x) (x) 35 | #endif 36 | 37 | #if !defined(HAVE_CONFIG_H) 38 | // clang-3.3 and gcc-4.3 have builtin functions for swap32/swap64 39 | #if LOCAL_GCC_PREREQ(4,3) || LOCAL_CLANG_PREREQ(3,3) 40 | #define HAVE_BUILTIN_BSWAP32 41 | #define HAVE_BUILTIN_BSWAP64 42 | #endif 43 | // clang-3.3 and gcc-4.8 have a builtin function for swap16 44 | #if LOCAL_GCC_PREREQ(4,8) || LOCAL_CLANG_PREREQ(3,3) 45 | #define HAVE_BUILTIN_BSWAP16 46 | #endif 47 | #endif // !HAVE_CONFIG_H 48 | 49 | static WEBP_INLINE uint16_t BSwap16(uint16_t x) { 50 | #if defined(HAVE_BUILTIN_BSWAP16) 51 | return __builtin_bswap16(x); 52 | #elif defined(_MSC_VER) 53 | return _byteswap_ushort(x); 54 | #else 55 | // gcc will recognize a 'rorw $8, ...' here: 56 | return (x >> 8) | ((x & 0xff) << 8); 57 | #endif // HAVE_BUILTIN_BSWAP16 58 | } 59 | 60 | static WEBP_INLINE uint32_t BSwap32(uint32_t x) { 61 | #if defined(WEBP_USE_MIPS32_R2) 62 | uint32_t ret; 63 | __asm__ volatile ( 64 | "wsbh %[ret], %[x] \n\t" 65 | "rotr %[ret], %[ret], 16 \n\t" 66 | : [ret]"=r"(ret) 67 | : [x]"r"(x) 68 | ); 69 | return ret; 70 | #elif defined(HAVE_BUILTIN_BSWAP32) 71 | return __builtin_bswap32(x); 72 | #elif defined(__i386__) || defined(__x86_64__) 73 | uint32_t swapped_bytes; 74 | __asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x)); 75 | return swapped_bytes; 76 | #elif defined(_MSC_VER) 77 | return (uint32_t)_byteswap_ulong(x); 78 | #else 79 | return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24); 80 | #endif // HAVE_BUILTIN_BSWAP32 81 | } 82 | 83 | static WEBP_INLINE uint64_t BSwap64(uint64_t x) { 84 | #if defined(HAVE_BUILTIN_BSWAP64) 85 | return __builtin_bswap64(x); 86 | #elif defined(__x86_64__) 87 | uint64_t swapped_bytes; 88 | __asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x)); 89 | return swapped_bytes; 90 | #elif defined(_MSC_VER) 91 | return (uint64_t)_byteswap_uint64(x); 92 | #else // generic code for swapping 64-bit values (suggested by bdb@) 93 | x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32); 94 | x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16); 95 | x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8); 96 | return x; 97 | #endif // HAVE_BUILTIN_BSWAP64 98 | } 99 | 100 | #endif // WEBP_UTILS_ENDIAN_INL_H_ 101 | -------------------------------------------------------------------------------- /libwebp/src/utils/filters.h: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Spatial prediction using various filters 11 | // 12 | // Author: Urvang (urvang@google.com) 13 | 14 | #ifndef WEBP_UTILS_FILTERS_H_ 15 | #define WEBP_UTILS_FILTERS_H_ 16 | 17 | #include "../webp/types.h" 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | // Filters. 24 | typedef enum { 25 | WEBP_FILTER_NONE = 0, 26 | WEBP_FILTER_HORIZONTAL, 27 | WEBP_FILTER_VERTICAL, 28 | WEBP_FILTER_GRADIENT, 29 | WEBP_FILTER_LAST = WEBP_FILTER_GRADIENT + 1, // end marker 30 | WEBP_FILTER_BEST, 31 | WEBP_FILTER_FAST 32 | } WEBP_FILTER_TYPE; 33 | 34 | typedef void (*WebPFilterFunc)(const uint8_t* in, int width, int height, 35 | int stride, uint8_t* out); 36 | typedef void (*WebPUnfilterFunc)(int width, int height, int stride, 37 | int row, int num_rows, uint8_t* data); 38 | 39 | // Filter the given data using the given predictor. 40 | // 'in' corresponds to a 2-dimensional pixel array of size (stride * height) 41 | // in raster order. 42 | // 'stride' is number of bytes per scan line (with possible padding). 43 | // 'out' should be pre-allocated. 44 | extern const WebPFilterFunc WebPFilters[WEBP_FILTER_LAST]; 45 | 46 | // In-place reconstruct the original data from the given filtered data. 47 | // The reconstruction will be done for 'num_rows' rows starting from 'row' 48 | // (assuming rows upto 'row - 1' are already reconstructed). 49 | extern const WebPUnfilterFunc WebPUnfilters[WEBP_FILTER_LAST]; 50 | 51 | // Fast estimate of a potentially good filter. 52 | WEBP_FILTER_TYPE EstimateBestFilter(const uint8_t* data, 53 | int width, int height, int stride); 54 | 55 | #ifdef __cplusplus 56 | } // extern "C" 57 | #endif 58 | 59 | #endif /* WEBP_UTILS_FILTERS_H_ */ 60 | -------------------------------------------------------------------------------- /libwebp/src/utils/huffman.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Utilities for building and looking up Huffman trees. 11 | // 12 | // Author: Urvang Joshi (urvang@google.com) 13 | 14 | #ifndef WEBP_UTILS_HUFFMAN_H_ 15 | #define WEBP_UTILS_HUFFMAN_H_ 16 | 17 | #include 18 | #include "../webp/format_constants.h" 19 | #include "../webp/types.h" 20 | 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | // A node of a Huffman tree. 26 | typedef struct { 27 | int symbol_; 28 | int children_; // delta offset to both children (contiguous) or 0 if leaf. 29 | } HuffmanTreeNode; 30 | 31 | // Huffman Tree. 32 | #define HUFF_LUT_BITS 7 33 | #define HUFF_LUT (1U << HUFF_LUT_BITS) 34 | typedef struct HuffmanTree HuffmanTree; 35 | struct HuffmanTree { 36 | // Fast lookup for short bit lengths. 37 | uint8_t lut_bits_[HUFF_LUT]; 38 | int16_t lut_symbol_[HUFF_LUT]; 39 | int16_t lut_jump_[HUFF_LUT]; 40 | // Complete tree for lookups. 41 | HuffmanTreeNode* root_; // all the nodes, starting at root. 42 | int max_nodes_; // max number of nodes 43 | int num_nodes_; // number of currently occupied nodes 44 | }; 45 | 46 | // Huffman Tree group. 47 | typedef struct HTreeGroup HTreeGroup; 48 | struct HTreeGroup { 49 | HuffmanTree htrees_[HUFFMAN_CODES_PER_META_CODE]; 50 | }; 51 | 52 | // Returns true if the given node is not a leaf of the Huffman tree. 53 | static WEBP_INLINE int HuffmanTreeNodeIsNotLeaf( 54 | const HuffmanTreeNode* const node) { 55 | return node->children_; 56 | } 57 | 58 | // Go down one level. Most critical function. 'right_child' must be 0 or 1. 59 | static WEBP_INLINE const HuffmanTreeNode* HuffmanTreeNextNode( 60 | const HuffmanTreeNode* node, int right_child) { 61 | return node + node->children_ + right_child; 62 | } 63 | 64 | // Releases the nodes of the Huffman tree. 65 | // Note: It does NOT free 'tree' itself. 66 | void VP8LHuffmanTreeFree(HuffmanTree* const tree); 67 | 68 | // Creates the instance of HTreeGroup with specified number of tree-groups. 69 | HTreeGroup* VP8LHtreeGroupsNew(int num_htree_groups); 70 | 71 | // Releases the memory allocated for HTreeGroup. 72 | void VP8LHtreeGroupsFree(HTreeGroup* htree_groups, int num_htree_groups); 73 | 74 | // Builds Huffman tree assuming code lengths are implicitly in symbol order. 75 | // The 'huff_codes' and 'code_lengths' are pre-allocated temporary memory 76 | // buffers, used for creating the huffman tree. 77 | // Returns false in case of error (invalid tree or memory error). 78 | int VP8LHuffmanTreeBuildImplicit(HuffmanTree* const tree, 79 | const int* const code_lengths, 80 | int* const huff_codes, 81 | int code_lengths_size); 82 | 83 | // Build a Huffman tree with explicitly given lists of code lengths, codes 84 | // and symbols. Verifies that all symbols added are smaller than max_symbol. 85 | // Returns false in case of an invalid symbol, invalid tree or memory error. 86 | int VP8LHuffmanTreeBuildExplicit(HuffmanTree* const tree, 87 | const int* const code_lengths, 88 | const int* const codes, 89 | const int* const symbols, int max_symbol, 90 | int num_symbols); 91 | 92 | // Utility: converts Huffman code lengths to corresponding Huffman codes. 93 | // 'huff_codes' should be pre-allocated. 94 | // Returns false in case of error (memory allocation, invalid codes). 95 | int VP8LHuffmanCodeLengthsToCodes(const int* const code_lengths, 96 | int code_lengths_size, int* const huff_codes); 97 | 98 | #ifdef __cplusplus 99 | } // extern "C" 100 | #endif 101 | 102 | #endif // WEBP_UTILS_HUFFMAN_H_ 103 | -------------------------------------------------------------------------------- /libwebp/src/utils/huffman_encode.h: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Author: Jyrki Alakuijala (jyrki@google.com) 11 | // 12 | // Entropy encoding (Huffman) for webp lossless 13 | 14 | #ifndef WEBP_UTILS_HUFFMAN_ENCODE_H_ 15 | #define WEBP_UTILS_HUFFMAN_ENCODE_H_ 16 | 17 | #include "../webp/types.h" 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | // Struct for holding the tree header in coded form. 24 | typedef struct { 25 | uint8_t code; // value (0..15) or escape code (16,17,18) 26 | uint8_t extra_bits; // extra bits for escape codes 27 | } HuffmanTreeToken; 28 | 29 | // Struct to represent the tree codes (depth and bits array). 30 | typedef struct { 31 | int num_symbols; // Number of symbols. 32 | uint8_t* code_lengths; // Code lengths of the symbols. 33 | uint16_t* codes; // Symbol Codes. 34 | } HuffmanTreeCode; 35 | 36 | // Struct to represent the Huffman tree. 37 | // TODO(vikasa): Add comment for the fields of the Struct. 38 | typedef struct { 39 | uint32_t total_count_; 40 | int value_; 41 | int pool_index_left_; // Index for the left sub-tree. 42 | int pool_index_right_; // Index for the right sub-tree. 43 | } HuffmanTree; 44 | 45 | // Turn the Huffman tree into a token sequence. 46 | // Returns the number of tokens used. 47 | int VP8LCreateCompressedHuffmanTree(const HuffmanTreeCode* const tree, 48 | HuffmanTreeToken* tokens, int max_tokens); 49 | 50 | // Create an optimized tree, and tokenize it. 51 | // 'buf_rle' and 'huff_tree' are pre-allocated and the 'tree' is the constructed 52 | // huffman code tree. 53 | void VP8LCreateHuffmanTree(uint32_t* const histogram, int tree_depth_limit, 54 | uint8_t* const buf_rle, HuffmanTree* const huff_tree, 55 | HuffmanTreeCode* const tree); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif // WEBP_UTILS_HUFFMAN_ENCODE_H_ 62 | -------------------------------------------------------------------------------- /libwebp/src/utils/quant_levels.c: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Quantize levels for specified number of quantization-levels ([2, 256]). 11 | // Min and max values are preserved (usual 0 and 255 for alpha plane). 12 | // 13 | // Author: Skal (pascal.massimino@gmail.com) 14 | 15 | #include 16 | 17 | #include "./quant_levels.h" 18 | 19 | #define NUM_SYMBOLS 256 20 | 21 | #define MAX_ITER 6 // Maximum number of convergence steps. 22 | #define ERROR_THRESHOLD 1e-4 // MSE stopping criterion. 23 | 24 | // ----------------------------------------------------------------------------- 25 | // Quantize levels. 26 | 27 | int QuantizeLevels(uint8_t* const data, int width, int height, 28 | int num_levels, uint64_t* const sse) { 29 | int freq[NUM_SYMBOLS] = { 0 }; 30 | int q_level[NUM_SYMBOLS] = { 0 }; 31 | double inv_q_level[NUM_SYMBOLS] = { 0 }; 32 | int min_s = 255, max_s = 0; 33 | const size_t data_size = height * width; 34 | int i, num_levels_in, iter; 35 | double last_err = 1.e38, err = 0.; 36 | const double err_threshold = ERROR_THRESHOLD * data_size; 37 | 38 | if (data == NULL) { 39 | return 0; 40 | } 41 | 42 | if (width <= 0 || height <= 0) { 43 | return 0; 44 | } 45 | 46 | if (num_levels < 2 || num_levels > 256) { 47 | return 0; 48 | } 49 | 50 | { 51 | size_t n; 52 | num_levels_in = 0; 53 | for (n = 0; n < data_size; ++n) { 54 | num_levels_in += (freq[data[n]] == 0); 55 | if (min_s > data[n]) min_s = data[n]; 56 | if (max_s < data[n]) max_s = data[n]; 57 | ++freq[data[n]]; 58 | } 59 | } 60 | 61 | if (num_levels_in <= num_levels) goto End; // nothing to do! 62 | 63 | // Start with uniformly spread centroids. 64 | for (i = 0; i < num_levels; ++i) { 65 | inv_q_level[i] = min_s + (double)(max_s - min_s) * i / (num_levels - 1); 66 | } 67 | 68 | // Fixed values. Won't be changed. 69 | q_level[min_s] = 0; 70 | q_level[max_s] = num_levels - 1; 71 | assert(inv_q_level[0] == min_s); 72 | assert(inv_q_level[num_levels - 1] == max_s); 73 | 74 | // k-Means iterations. 75 | for (iter = 0; iter < MAX_ITER; ++iter) { 76 | double q_sum[NUM_SYMBOLS] = { 0 }; 77 | double q_count[NUM_SYMBOLS] = { 0 }; 78 | int s, slot = 0; 79 | 80 | // Assign classes to representatives. 81 | for (s = min_s; s <= max_s; ++s) { 82 | // Keep track of the nearest neighbour 'slot' 83 | while (slot < num_levels - 1 && 84 | 2 * s > inv_q_level[slot] + inv_q_level[slot + 1]) { 85 | ++slot; 86 | } 87 | if (freq[s] > 0) { 88 | q_sum[slot] += s * freq[s]; 89 | q_count[slot] += freq[s]; 90 | } 91 | q_level[s] = slot; 92 | } 93 | 94 | // Assign new representatives to classes. 95 | if (num_levels > 2) { 96 | for (slot = 1; slot < num_levels - 1; ++slot) { 97 | const double count = q_count[slot]; 98 | if (count > 0.) { 99 | inv_q_level[slot] = q_sum[slot] / count; 100 | } 101 | } 102 | } 103 | 104 | // Compute convergence error. 105 | err = 0.; 106 | for (s = min_s; s <= max_s; ++s) { 107 | const double error = s - inv_q_level[q_level[s]]; 108 | err += freq[s] * error * error; 109 | } 110 | 111 | // Check for convergence: we stop as soon as the error is no 112 | // longer improving. 113 | if (last_err - err < err_threshold) break; 114 | last_err = err; 115 | } 116 | 117 | // Remap the alpha plane to quantized values. 118 | { 119 | // double->int rounding operation can be costly, so we do it 120 | // once for all before remapping. We also perform the data[] -> slot 121 | // mapping, while at it (avoid one indirection in the final loop). 122 | uint8_t map[NUM_SYMBOLS]; 123 | int s; 124 | size_t n; 125 | for (s = min_s; s <= max_s; ++s) { 126 | const int slot = q_level[s]; 127 | map[s] = (uint8_t)(inv_q_level[slot] + .5); 128 | } 129 | // Final pass. 130 | for (n = 0; n < data_size; ++n) { 131 | data[n] = map[data[n]]; 132 | } 133 | } 134 | End: 135 | // Store sum of squared error if needed. 136 | if (sse != NULL) *sse = (uint64_t)err; 137 | 138 | return 1; 139 | } 140 | 141 | -------------------------------------------------------------------------------- /libwebp/src/utils/quant_levels.h: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Alpha plane quantization utility 11 | // 12 | // Author: Vikas Arora (vikasa@google.com) 13 | 14 | #ifndef WEBP_UTILS_QUANT_LEVELS_H_ 15 | #define WEBP_UTILS_QUANT_LEVELS_H_ 16 | 17 | #include 18 | 19 | #include "../webp/types.h" 20 | 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | // Replace the input 'data' of size 'width'x'height' with 'num-levels' 26 | // quantized values. If not NULL, 'sse' will contain the sum of squared error. 27 | // Valid range for 'num_levels' is [2, 256]. 28 | // Returns false in case of error (data is NULL, or parameters are invalid). 29 | int QuantizeLevels(uint8_t* const data, int width, int height, int num_levels, 30 | uint64_t* const sse); 31 | 32 | #ifdef __cplusplus 33 | } // extern "C" 34 | #endif 35 | 36 | #endif /* WEBP_UTILS_QUANT_LEVELS_H_ */ 37 | -------------------------------------------------------------------------------- /libwebp/src/utils/quant_levels_dec.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Alpha plane de-quantization utility 11 | // 12 | // Author: Vikas Arora (vikasa@google.com) 13 | 14 | #ifndef WEBP_UTILS_QUANT_LEVELS_DEC_H_ 15 | #define WEBP_UTILS_QUANT_LEVELS_DEC_H_ 16 | 17 | #include "../webp/types.h" 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | // Apply post-processing to input 'data' of size 'width'x'height' assuming that 24 | // the source was quantized to a reduced number of levels. 25 | // Strength is in [0..100] and controls the amount of dithering applied. 26 | // Returns false in case of error (data is NULL, invalid parameters, 27 | // malloc failure, ...). 28 | int WebPDequantizeLevels(uint8_t* const data, int width, int height, 29 | int strength); 30 | 31 | #ifdef __cplusplus 32 | } // extern "C" 33 | #endif 34 | 35 | #endif /* WEBP_UTILS_QUANT_LEVELS_DEC_H_ */ 36 | -------------------------------------------------------------------------------- /libwebp/src/utils/random.c: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Pseudo-random utilities 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #include 15 | #include "./random.h" 16 | 17 | //------------------------------------------------------------------------------ 18 | 19 | // 31b-range values 20 | static const uint32_t kRandomTable[VP8_RANDOM_TABLE_SIZE] = { 21 | 0x0de15230, 0x03b31886, 0x775faccb, 0x1c88626a, 0x68385c55, 0x14b3b828, 22 | 0x4a85fef8, 0x49ddb84b, 0x64fcf397, 0x5c550289, 0x4a290000, 0x0d7ec1da, 23 | 0x5940b7ab, 0x5492577d, 0x4e19ca72, 0x38d38c69, 0x0c01ee65, 0x32a1755f, 24 | 0x5437f652, 0x5abb2c32, 0x0faa57b1, 0x73f533e7, 0x685feeda, 0x7563cce2, 25 | 0x6e990e83, 0x4730a7ed, 0x4fc0d9c6, 0x496b153c, 0x4f1403fa, 0x541afb0c, 26 | 0x73990b32, 0x26d7cb1c, 0x6fcc3706, 0x2cbb77d8, 0x75762f2a, 0x6425ccdd, 27 | 0x24b35461, 0x0a7d8715, 0x220414a8, 0x141ebf67, 0x56b41583, 0x73e502e3, 28 | 0x44cab16f, 0x28264d42, 0x73baaefb, 0x0a50ebed, 0x1d6ab6fb, 0x0d3ad40b, 29 | 0x35db3b68, 0x2b081e83, 0x77ce6b95, 0x5181e5f0, 0x78853bbc, 0x009f9494, 30 | 0x27e5ed3c 31 | }; 32 | 33 | void VP8InitRandom(VP8Random* const rg, float dithering) { 34 | memcpy(rg->tab_, kRandomTable, sizeof(rg->tab_)); 35 | rg->index1_ = 0; 36 | rg->index2_ = 31; 37 | rg->amp_ = (dithering < 0.0) ? 0 38 | : (dithering > 1.0) ? (1 << VP8_RANDOM_DITHER_FIX) 39 | : (uint32_t)((1 << VP8_RANDOM_DITHER_FIX) * dithering); 40 | } 41 | 42 | //------------------------------------------------------------------------------ 43 | 44 | -------------------------------------------------------------------------------- /libwebp/src/utils/random.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Pseudo-random utilities 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #ifndef WEBP_UTILS_RANDOM_H_ 15 | #define WEBP_UTILS_RANDOM_H_ 16 | 17 | #include 18 | #include "../webp/types.h" 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #define VP8_RANDOM_DITHER_FIX 8 // fixed-point precision for dithering 25 | #define VP8_RANDOM_TABLE_SIZE 55 26 | 27 | typedef struct { 28 | int index1_, index2_; 29 | uint32_t tab_[VP8_RANDOM_TABLE_SIZE]; 30 | int amp_; 31 | } VP8Random; 32 | 33 | // Initializes random generator with an amplitude 'dithering' in range [0..1]. 34 | void VP8InitRandom(VP8Random* const rg, float dithering); 35 | 36 | // Returns a centered pseudo-random number with 'num_bits' amplitude. 37 | // (uses D.Knuth's Difference-based random generator). 38 | // 'amp' is in VP8_RANDOM_DITHER_FIX fixed-point precision. 39 | static WEBP_INLINE int VP8RandomBits2(VP8Random* const rg, int num_bits, 40 | int amp) { 41 | int diff; 42 | assert(num_bits + VP8_RANDOM_DITHER_FIX <= 31); 43 | diff = rg->tab_[rg->index1_] - rg->tab_[rg->index2_]; 44 | if (diff < 0) diff += (1u << 31); 45 | rg->tab_[rg->index1_] = diff; 46 | if (++rg->index1_ == VP8_RANDOM_TABLE_SIZE) rg->index1_ = 0; 47 | if (++rg->index2_ == VP8_RANDOM_TABLE_SIZE) rg->index2_ = 0; 48 | // sign-extend, 0-center 49 | diff = (int)((uint32_t)diff << 1) >> (32 - num_bits); 50 | diff = (diff * amp) >> VP8_RANDOM_DITHER_FIX; // restrict range 51 | diff += 1 << (num_bits - 1); // shift back to 0.5-center 52 | return diff; 53 | } 54 | 55 | static WEBP_INLINE int VP8RandomBits(VP8Random* const rg, int num_bits) { 56 | return VP8RandomBits2(rg, num_bits, rg->amp_); 57 | } 58 | 59 | #ifdef __cplusplus 60 | } // extern "C" 61 | #endif 62 | 63 | #endif /* WEBP_UTILS_RANDOM_H_ */ 64 | -------------------------------------------------------------------------------- /libwebp/src/utils/rescaler.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Rescaling functions 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #ifndef WEBP_UTILS_RESCALER_H_ 15 | #define WEBP_UTILS_RESCALER_H_ 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | #include "../webp/types.h" 22 | 23 | // Structure used for on-the-fly rescaling 24 | typedef struct { 25 | int x_expand; // true if we're expanding in the x direction 26 | int num_channels; // bytes to jump between pixels 27 | int fy_scale, fx_scale; // fixed-point scaling factor 28 | int64_t fxy_scale; // '' 29 | // we need hpel-precise add/sub increments, for the downsampled U/V planes. 30 | int y_accum; // vertical accumulator 31 | int y_add, y_sub; // vertical increments (add ~= src, sub ~= dst) 32 | int x_add, x_sub; // horizontal increments (add ~= src, sub ~= dst) 33 | int src_width, src_height; // source dimensions 34 | int dst_width, dst_height; // destination dimensions 35 | uint8_t* dst; 36 | int dst_stride; 37 | int32_t* irow, *frow; // work buffer 38 | } WebPRescaler; 39 | 40 | // Initialize a rescaler given scratch area 'work' and dimensions of src & dst. 41 | void WebPRescalerInit(WebPRescaler* const rescaler, 42 | int src_width, int src_height, 43 | uint8_t* const dst, 44 | int dst_width, int dst_height, int dst_stride, 45 | int num_channels, 46 | int x_add, int x_sub, 47 | int y_add, int y_sub, 48 | int32_t* const work); 49 | 50 | // Returns the number of input lines needed next to produce one output line, 51 | // considering that the maximum available input lines are 'max_num_lines'. 52 | int WebPRescaleNeededLines(const WebPRescaler* const rescaler, 53 | int max_num_lines); 54 | 55 | // Import multiple rows over all channels, until at least one row is ready to 56 | // be exported. Returns the actual number of lines that were imported. 57 | int WebPRescalerImport(WebPRescaler* const rescaler, int num_rows, 58 | const uint8_t* src, int src_stride); 59 | 60 | // Import a row of data and save its contribution in the rescaler. 61 | // 'channel' denotes the channel number to be imported. 62 | extern void (*WebPRescalerImportRow)(WebPRescaler* const wrk, 63 | const uint8_t* const src, int channel); 64 | // Export one row (starting at x_out position) from rescaler. 65 | extern void (*WebPRescalerExportRow)(WebPRescaler* const wrk, int x_out); 66 | 67 | // Return true if there is pending output rows ready. 68 | static WEBP_INLINE 69 | int WebPRescalerHasPendingOutput(const WebPRescaler* const rescaler) { 70 | return (rescaler->y_accum <= 0); 71 | } 72 | 73 | // Export as many rows as possible. Return the numbers of rows written. 74 | int WebPRescalerExport(WebPRescaler* const rescaler); 75 | 76 | //------------------------------------------------------------------------------ 77 | 78 | #ifdef __cplusplus 79 | } // extern "C" 80 | #endif 81 | 82 | #endif /* WEBP_UTILS_RESCALER_H_ */ 83 | -------------------------------------------------------------------------------- /libwebp/src/utils/thread.h: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Multi-threaded worker 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #ifndef WEBP_UTILS_THREAD_H_ 15 | #define WEBP_UTILS_THREAD_H_ 16 | 17 | #ifdef HAVE_CONFIG_H 18 | #include "../webp/config.h" 19 | #endif 20 | 21 | #include "../webp/types.h" 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | // State of the worker thread object 28 | typedef enum { 29 | NOT_OK = 0, // object is unusable 30 | OK, // ready to work 31 | WORK // busy finishing the current task 32 | } WebPWorkerStatus; 33 | 34 | // Function to be called by the worker thread. Takes two opaque pointers as 35 | // arguments (data1 and data2), and should return false in case of error. 36 | typedef int (*WebPWorkerHook)(void*, void*); 37 | 38 | // Platform-dependent implementation details for the worker. 39 | typedef struct WebPWorkerImpl WebPWorkerImpl; 40 | 41 | // Synchronization object used to launch job in the worker thread 42 | typedef struct { 43 | WebPWorkerImpl* impl_; 44 | WebPWorkerStatus status_; 45 | WebPWorkerHook hook; // hook to call 46 | void* data1; // first argument passed to 'hook' 47 | void* data2; // second argument passed to 'hook' 48 | int had_error; // return value of the last call to 'hook' 49 | } WebPWorker; 50 | 51 | // The interface for all thread-worker related functions. All these functions 52 | // must be implemented. 53 | typedef struct { 54 | // Must be called first, before any other method. 55 | void (*Init)(WebPWorker* const worker); 56 | // Must be called to initialize the object and spawn the thread. Re-entrant. 57 | // Will potentially launch the thread. Returns false in case of error. 58 | int (*Reset)(WebPWorker* const worker); 59 | // Makes sure the previous work is finished. Returns true if worker->had_error 60 | // was not set and no error condition was triggered by the working thread. 61 | int (*Sync)(WebPWorker* const worker); 62 | // Triggers the thread to call hook() with data1 and data2 arguments. These 63 | // hook/data1/data2 values can be changed at any time before calling this 64 | // function, but not be changed afterward until the next call to Sync(). 65 | void (*Launch)(WebPWorker* const worker); 66 | // This function is similar to Launch() except that it calls the 67 | // hook directly instead of using a thread. Convenient to bypass the thread 68 | // mechanism while still using the WebPWorker structs. Sync() must 69 | // still be called afterward (for error reporting). 70 | void (*Execute)(WebPWorker* const worker); 71 | // Kill the thread and terminate the object. To use the object again, one 72 | // must call Reset() again. 73 | void (*End)(WebPWorker* const worker); 74 | } WebPWorkerInterface; 75 | 76 | // Install a new set of threading functions, overriding the defaults. This 77 | // should be done before any workers are started, i.e., before any encoding or 78 | // decoding takes place. The contents of the interface struct are copied, it 79 | // is safe to free the corresponding memory after this call. This function is 80 | // not thread-safe. Return false in case of invalid pointer or methods. 81 | WEBP_EXTERN(int) WebPSetWorkerInterface( 82 | const WebPWorkerInterface* const interface); 83 | 84 | // Retrieve the currently set thread worker interface. 85 | WEBP_EXTERN(const WebPWorkerInterface*) WebPGetWorkerInterface(void); 86 | 87 | //------------------------------------------------------------------------------ 88 | 89 | #ifdef __cplusplus 90 | } // extern "C" 91 | #endif 92 | 93 | #endif /* WEBP_UTILS_THREAD_H_ */ 94 | -------------------------------------------------------------------------------- /libwebp/src/utils/utils.c: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Misc. common utility functions 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #include 15 | #include "./utils.h" 16 | 17 | // If PRINT_MEM_INFO is defined, extra info (like total memory used, number of 18 | // alloc/free etc) is printed. For debugging/tuning purpose only (it's slow, 19 | // and not multi-thread safe!). 20 | // An interesting alternative is valgrind's 'massif' tool: 21 | // http://valgrind.org/docs/manual/ms-manual.html 22 | // Here is an example command line: 23 | /* valgrind --tool=massif --massif-out-file=massif.out \ 24 | --stacks=yes --alloc-fn=WebPSafeAlloc --alloc-fn=WebPSafeCalloc 25 | ms_print massif.out 26 | */ 27 | // In addition: 28 | // * if PRINT_MEM_TRAFFIC is defined, all the details of the malloc/free cycles 29 | // are printed. 30 | // * if MALLOC_FAIL_AT is defined, the global environment variable 31 | // $MALLOC_FAIL_AT is used to simulate a memory error when calloc or malloc 32 | // is called for the nth time. Example usage: 33 | // export MALLOC_FAIL_AT=50 && ./examples/cwebp input.png 34 | // * if MALLOC_LIMIT is defined, the global environment variable $MALLOC_LIMIT 35 | // sets the maximum amount of memory (in bytes) made available to libwebp. 36 | // This can be used to emulate environment with very limited memory. 37 | // Example: export MALLOC_LIMIT=64000000 && ./examples/dwebp picture.webp 38 | 39 | // #define PRINT_MEM_INFO 40 | // #define PRINT_MEM_TRAFFIC 41 | // #define MALLOC_FAIL_AT 42 | // #define MALLOC_LIMIT 43 | 44 | //------------------------------------------------------------------------------ 45 | // Checked memory allocation 46 | 47 | #if defined(PRINT_MEM_INFO) 48 | 49 | #include 50 | #include // for abort() 51 | 52 | static int num_malloc_calls = 0; 53 | static int num_calloc_calls = 0; 54 | static int num_free_calls = 0; 55 | static int countdown_to_fail = 0; // 0 = off 56 | 57 | typedef struct MemBlock MemBlock; 58 | struct MemBlock { 59 | void* ptr_; 60 | size_t size_; 61 | MemBlock* next_; 62 | }; 63 | 64 | static MemBlock* all_blocks = NULL; 65 | static size_t total_mem = 0; 66 | static size_t total_mem_allocated = 0; 67 | static size_t high_water_mark = 0; 68 | static size_t mem_limit = 0; 69 | 70 | static int exit_registered = 0; 71 | 72 | static void PrintMemInfo(void) { 73 | fprintf(stderr, "\nMEMORY INFO:\n"); 74 | fprintf(stderr, "num calls to: malloc = %4d\n", num_malloc_calls); 75 | fprintf(stderr, " calloc = %4d\n", num_calloc_calls); 76 | fprintf(stderr, " free = %4d\n", num_free_calls); 77 | fprintf(stderr, "total_mem: %u\n", (uint32_t)total_mem); 78 | fprintf(stderr, "total_mem allocated: %u\n", (uint32_t)total_mem_allocated); 79 | fprintf(stderr, "high-water mark: %u\n", (uint32_t)high_water_mark); 80 | while (all_blocks != NULL) { 81 | MemBlock* b = all_blocks; 82 | all_blocks = b->next_; 83 | free(b); 84 | } 85 | } 86 | 87 | static void Increment(int* const v) { 88 | if (!exit_registered) { 89 | #if defined(MALLOC_FAIL_AT) 90 | { 91 | const char* const malloc_fail_at_str = getenv("MALLOC_FAIL_AT"); 92 | if (malloc_fail_at_str != NULL) { 93 | countdown_to_fail = atoi(malloc_fail_at_str); 94 | } 95 | } 96 | #endif 97 | #if defined(MALLOC_LIMIT) 98 | { 99 | const char* const malloc_limit_str = getenv("MALLOC_LIMIT"); 100 | if (malloc_limit_str != NULL) { 101 | mem_limit = atoi(malloc_limit_str); 102 | } 103 | } 104 | #endif 105 | (void)countdown_to_fail; 106 | (void)mem_limit; 107 | atexit(PrintMemInfo); 108 | exit_registered = 1; 109 | } 110 | ++*v; 111 | } 112 | 113 | static void AddMem(void* ptr, size_t size) { 114 | if (ptr != NULL) { 115 | MemBlock* const b = (MemBlock*)malloc(sizeof(*b)); 116 | if (b == NULL) abort(); 117 | b->next_ = all_blocks; 118 | all_blocks = b; 119 | b->ptr_ = ptr; 120 | b->size_ = size; 121 | total_mem += size; 122 | total_mem_allocated += size; 123 | #if defined(PRINT_MEM_TRAFFIC) 124 | #if defined(MALLOC_FAIL_AT) 125 | fprintf(stderr, "fail-count: %5d [mem=%u]\n", 126 | num_malloc_calls + num_calloc_calls, (uint32_t)total_mem); 127 | #else 128 | fprintf(stderr, "Mem: %u (+%u)\n", (uint32_t)total_mem, (uint32_t)size); 129 | #endif 130 | #endif 131 | if (total_mem > high_water_mark) high_water_mark = total_mem; 132 | } 133 | } 134 | 135 | static void SubMem(void* ptr) { 136 | if (ptr != NULL) { 137 | MemBlock** b = &all_blocks; 138 | // Inefficient search, but that's just for debugging. 139 | while (*b != NULL && (*b)->ptr_ != ptr) b = &(*b)->next_; 140 | if (*b == NULL) { 141 | fprintf(stderr, "Invalid pointer free! (%p)\n", ptr); 142 | abort(); 143 | } 144 | { 145 | MemBlock* const block = *b; 146 | *b = block->next_; 147 | total_mem -= block->size_; 148 | #if defined(PRINT_MEM_TRAFFIC) 149 | fprintf(stderr, "Mem: %u (-%u)\n", 150 | (uint32_t)total_mem, (uint32_t)block->size_); 151 | #endif 152 | free(block); 153 | } 154 | } 155 | } 156 | 157 | #else 158 | #define Increment(v) do {} while (0) 159 | #define AddMem(p, s) do {} while (0) 160 | #define SubMem(p) do {} while (0) 161 | #endif 162 | 163 | // Returns 0 in case of overflow of nmemb * size. 164 | static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) { 165 | const uint64_t total_size = nmemb * size; 166 | if (nmemb == 0) return 1; 167 | if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0; 168 | if (total_size != (size_t)total_size) return 0; 169 | #if defined(PRINT_MEM_INFO) && defined(MALLOC_FAIL_AT) 170 | if (countdown_to_fail > 0 && --countdown_to_fail == 0) { 171 | return 0; // fake fail! 172 | } 173 | #endif 174 | #if defined(MALLOC_LIMIT) 175 | if (mem_limit > 0 && total_mem + total_size >= mem_limit) { 176 | return 0; // fake fail! 177 | } 178 | #endif 179 | 180 | return 1; 181 | } 182 | 183 | void* WebPSafeMalloc(uint64_t nmemb, size_t size) { 184 | void* ptr; 185 | Increment(&num_malloc_calls); 186 | if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL; 187 | assert(nmemb * size > 0); 188 | ptr = malloc((size_t)(nmemb * size)); 189 | AddMem(ptr, (size_t)(nmemb * size)); 190 | return ptr; 191 | } 192 | 193 | void* WebPSafeCalloc(uint64_t nmemb, size_t size) { 194 | void* ptr; 195 | Increment(&num_calloc_calls); 196 | if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL; 197 | assert(nmemb * size > 0); 198 | ptr = calloc((size_t)nmemb, size); 199 | AddMem(ptr, (size_t)(nmemb * size)); 200 | return ptr; 201 | } 202 | 203 | void WebPSafeFree(void* const ptr) { 204 | if (ptr != NULL) { 205 | Increment(&num_free_calls); 206 | SubMem(ptr); 207 | } 208 | free(ptr); 209 | } 210 | 211 | //------------------------------------------------------------------------------ 212 | -------------------------------------------------------------------------------- /libwebp/src/utils/utils.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Misc. common utility functions 11 | // 12 | // Authors: Skal (pascal.massimino@gmail.com) 13 | // Urvang (urvang@google.com) 14 | 15 | #ifndef WEBP_UTILS_UTILS_H_ 16 | #define WEBP_UTILS_UTILS_H_ 17 | 18 | #include 19 | 20 | #include "../webp/types.h" 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | //------------------------------------------------------------------------------ 27 | // Memory allocation 28 | 29 | // This is the maximum memory amount that libwebp will ever try to allocate. 30 | #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 40) 31 | 32 | // size-checking safe malloc/calloc: verify that the requested size is not too 33 | // large, or return NULL. You don't need to call these for constructs like 34 | // malloc(sizeof(foo)), but only if there's picture-dependent size involved 35 | // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this 36 | // safe malloc() borrows the signature from calloc(), pointing at the dangerous 37 | // underlying multiply involved. 38 | WEBP_EXTERN(void*) WebPSafeMalloc(uint64_t nmemb, size_t size); 39 | // Note that WebPSafeCalloc() expects the second argument type to be 'size_t' 40 | // in order to favor the "calloc(num_foo, sizeof(foo))" pattern. 41 | WEBP_EXTERN(void*) WebPSafeCalloc(uint64_t nmemb, size_t size); 42 | 43 | // Companion deallocation function to the above allocations. 44 | WEBP_EXTERN(void) WebPSafeFree(void* const ptr); 45 | 46 | //------------------------------------------------------------------------------ 47 | // Reading/writing data. 48 | 49 | // Read 16, 24 or 32 bits stored in little-endian order. 50 | static WEBP_INLINE int GetLE16(const uint8_t* const data) { 51 | return (int)(data[0] << 0) | (data[1] << 8); 52 | } 53 | 54 | static WEBP_INLINE int GetLE24(const uint8_t* const data) { 55 | return GetLE16(data) | (data[2] << 16); 56 | } 57 | 58 | static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) { 59 | return (uint32_t)GetLE16(data) | (GetLE16(data + 2) << 16); 60 | } 61 | 62 | // Store 16, 24 or 32 bits in little-endian order. 63 | static WEBP_INLINE void PutLE16(uint8_t* const data, int val) { 64 | assert(val < (1 << 16)); 65 | data[0] = (val >> 0); 66 | data[1] = (val >> 8); 67 | } 68 | 69 | static WEBP_INLINE void PutLE24(uint8_t* const data, int val) { 70 | assert(val < (1 << 24)); 71 | PutLE16(data, val & 0xffff); 72 | data[2] = (val >> 16); 73 | } 74 | 75 | static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) { 76 | PutLE16(data, (int)(val & 0xffff)); 77 | PutLE16(data + 2, (int)(val >> 16)); 78 | } 79 | 80 | // Returns (int)floor(log2(n)). n must be > 0. 81 | // use GNU builtins where available. 82 | #if defined(__GNUC__) && \ 83 | ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4) 84 | static WEBP_INLINE int BitsLog2Floor(uint32_t n) { 85 | return 31 ^ __builtin_clz(n); 86 | } 87 | #elif defined(_MSC_VER) && _MSC_VER > 1310 && \ 88 | (defined(_M_X64) || defined(_M_IX86)) 89 | #include 90 | #pragma intrinsic(_BitScanReverse) 91 | 92 | static WEBP_INLINE int BitsLog2Floor(uint32_t n) { 93 | uint32_t first_set_bit; 94 | _BitScanReverse(&first_set_bit, n); 95 | return first_set_bit; 96 | } 97 | #else 98 | static WEBP_INLINE int BitsLog2Floor(uint32_t n) { 99 | int log = 0; 100 | uint32_t value = n; 101 | int i; 102 | 103 | for (i = 4; i >= 0; --i) { 104 | const int shift = (1 << i); 105 | const uint32_t x = value >> shift; 106 | if (x != 0) { 107 | value = x; 108 | log += shift; 109 | } 110 | } 111 | return log; 112 | } 113 | #endif 114 | 115 | //------------------------------------------------------------------------------ 116 | 117 | #ifdef __cplusplus 118 | } // extern "C" 119 | #endif 120 | 121 | #endif /* WEBP_UTILS_UTILS_H_ */ 122 | -------------------------------------------------------------------------------- /libwebp/src/webp/format_constants.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Internal header for constants related to WebP file format. 11 | // 12 | // Author: Urvang (urvang@google.com) 13 | 14 | #ifndef WEBP_WEBP_FORMAT_CONSTANTS_H_ 15 | #define WEBP_WEBP_FORMAT_CONSTANTS_H_ 16 | 17 | // Create fourcc of the chunk from the chunk tag characters. 18 | #define MKFOURCC(a, b, c, d) ((uint32_t)(a) | (b) << 8 | (c) << 16 | (d) << 24) 19 | 20 | // VP8 related constants. 21 | #define VP8_SIGNATURE 0x9d012a // Signature in VP8 data. 22 | #define VP8_MAX_PARTITION0_SIZE (1 << 19) // max size of mode partition 23 | #define VP8_MAX_PARTITION_SIZE (1 << 24) // max size for token partition 24 | #define VP8_FRAME_HEADER_SIZE 10 // Size of the frame header within VP8 data. 25 | 26 | // VP8L related constants. 27 | #define VP8L_SIGNATURE_SIZE 1 // VP8L signature size. 28 | #define VP8L_MAGIC_BYTE 0x2f // VP8L signature byte. 29 | #define VP8L_IMAGE_SIZE_BITS 14 // Number of bits used to store 30 | // width and height. 31 | #define VP8L_VERSION_BITS 3 // 3 bits reserved for version. 32 | #define VP8L_VERSION 0 // version 0 33 | #define VP8L_FRAME_HEADER_SIZE 5 // Size of the VP8L frame header. 34 | 35 | #define MAX_PALETTE_SIZE 256 36 | #define MAX_CACHE_BITS 11 37 | #define HUFFMAN_CODES_PER_META_CODE 5 38 | #define ARGB_BLACK 0xff000000 39 | 40 | #define DEFAULT_CODE_LENGTH 8 41 | #define MAX_ALLOWED_CODE_LENGTH 15 42 | 43 | #define NUM_LITERAL_CODES 256 44 | #define NUM_LENGTH_CODES 24 45 | #define NUM_DISTANCE_CODES 40 46 | #define CODE_LENGTH_CODES 19 47 | 48 | #define MIN_HUFFMAN_BITS 2 // min number of Huffman bits 49 | #define MAX_HUFFMAN_BITS 9 // max number of Huffman bits 50 | 51 | #define TRANSFORM_PRESENT 1 // The bit to be written when next data 52 | // to be read is a transform. 53 | #define NUM_TRANSFORMS 4 // Maximum number of allowed transform 54 | // in a bitstream. 55 | typedef enum { 56 | PREDICTOR_TRANSFORM = 0, 57 | CROSS_COLOR_TRANSFORM = 1, 58 | SUBTRACT_GREEN = 2, 59 | COLOR_INDEXING_TRANSFORM = 3 60 | } VP8LImageTransformType; 61 | 62 | // Alpha related constants. 63 | #define ALPHA_HEADER_LEN 1 64 | #define ALPHA_NO_COMPRESSION 0 65 | #define ALPHA_LOSSLESS_COMPRESSION 1 66 | #define ALPHA_PREPROCESSED_LEVELS 1 67 | 68 | // Mux related constants. 69 | #define TAG_SIZE 4 // Size of a chunk tag (e.g. "VP8L"). 70 | #define CHUNK_SIZE_BYTES 4 // Size needed to store chunk's size. 71 | #define CHUNK_HEADER_SIZE 8 // Size of a chunk header. 72 | #define RIFF_HEADER_SIZE 12 // Size of the RIFF header ("RIFFnnnnWEBP"). 73 | #define ANMF_CHUNK_SIZE 16 // Size of an ANMF chunk. 74 | #define ANIM_CHUNK_SIZE 6 // Size of an ANIM chunk. 75 | #define FRGM_CHUNK_SIZE 6 // Size of a FRGM chunk. 76 | #define VP8X_CHUNK_SIZE 10 // Size of a VP8X chunk. 77 | 78 | #define MAX_CANVAS_SIZE (1 << 24) // 24-bit max for VP8X width/height. 79 | #define MAX_IMAGE_AREA (1ULL << 32) // 32-bit max for width x height. 80 | #define MAX_LOOP_COUNT (1 << 16) // maximum value for loop-count 81 | #define MAX_DURATION (1 << 24) // maximum duration 82 | #define MAX_POSITION_OFFSET (1 << 24) // maximum frame/fragment x/y offset 83 | 84 | // Maximum chunk payload is such that adding the header and padding won't 85 | // overflow a uint32_t. 86 | #define MAX_CHUNK_PAYLOAD (~0U - CHUNK_HEADER_SIZE - 1) 87 | 88 | #endif /* WEBP_WEBP_FORMAT_CONSTANTS_H_ */ 89 | -------------------------------------------------------------------------------- /libwebp/src/webp/mux_types.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Data-types common to the mux and demux libraries. 11 | // 12 | // Author: Urvang (urvang@google.com) 13 | 14 | #ifndef WEBP_WEBP_MUX_TYPES_H_ 15 | #define WEBP_WEBP_MUX_TYPES_H_ 16 | 17 | #include // free() 18 | #include // memset() 19 | #include "./types.h" 20 | 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | // Note: forward declaring enumerations is not allowed in (strict) C and C++, 26 | // the types are left here for reference. 27 | // typedef enum WebPFeatureFlags WebPFeatureFlags; 28 | // typedef enum WebPMuxAnimDispose WebPMuxAnimDispose; 29 | // typedef enum WebPMuxAnimBlend WebPMuxAnimBlend; 30 | typedef struct WebPData WebPData; 31 | 32 | // VP8X Feature Flags. 33 | typedef enum WebPFeatureFlags { 34 | FRAGMENTS_FLAG = 0x00000001, 35 | ANIMATION_FLAG = 0x00000002, 36 | XMP_FLAG = 0x00000004, 37 | EXIF_FLAG = 0x00000008, 38 | ALPHA_FLAG = 0x00000010, 39 | ICCP_FLAG = 0x00000020 40 | } WebPFeatureFlags; 41 | 42 | // Dispose method (animation only). Indicates how the area used by the current 43 | // frame is to be treated before rendering the next frame on the canvas. 44 | typedef enum WebPMuxAnimDispose { 45 | WEBP_MUX_DISPOSE_NONE, // Do not dispose. 46 | WEBP_MUX_DISPOSE_BACKGROUND // Dispose to background color. 47 | } WebPMuxAnimDispose; 48 | 49 | // Blend operation (animation only). Indicates how transparent pixels of the 50 | // current frame are blended with those of the previous canvas. 51 | typedef enum WebPMuxAnimBlend { 52 | WEBP_MUX_BLEND, // Blend. 53 | WEBP_MUX_NO_BLEND // Do not blend. 54 | } WebPMuxAnimBlend; 55 | 56 | // Data type used to describe 'raw' data, e.g., chunk data 57 | // (ICC profile, metadata) and WebP compressed image data. 58 | struct WebPData { 59 | const uint8_t* bytes; 60 | size_t size; 61 | }; 62 | 63 | // Initializes the contents of the 'webp_data' object with default values. 64 | static WEBP_INLINE void WebPDataInit(WebPData* webp_data) { 65 | if (webp_data != NULL) { 66 | memset(webp_data, 0, sizeof(*webp_data)); 67 | } 68 | } 69 | 70 | // Clears the contents of the 'webp_data' object by calling free(). Does not 71 | // deallocate the object itself. 72 | static WEBP_INLINE void WebPDataClear(WebPData* webp_data) { 73 | if (webp_data != NULL) { 74 | free((void*)webp_data->bytes); 75 | WebPDataInit(webp_data); 76 | } 77 | } 78 | 79 | // Allocates necessary storage for 'dst' and copies the contents of 'src'. 80 | // Returns true on success. 81 | static WEBP_INLINE int WebPDataCopy(const WebPData* src, WebPData* dst) { 82 | if (src == NULL || dst == NULL) return 0; 83 | WebPDataInit(dst); 84 | if (src->bytes != NULL && src->size != 0) { 85 | dst->bytes = (uint8_t*)malloc(src->size); 86 | if (dst->bytes == NULL) return 0; 87 | memcpy((void*)dst->bytes, src->bytes, src->size); 88 | dst->size = src->size; 89 | } 90 | return 1; 91 | } 92 | 93 | #ifdef __cplusplus 94 | } // extern "C" 95 | #endif 96 | 97 | #endif /* WEBP_WEBP_MUX_TYPES_H_ */ 98 | -------------------------------------------------------------------------------- /libwebp/src/webp/types.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Google Inc. All Rights Reserved. 2 | // 3 | // Use of this source code is governed by a BSD-style license 4 | // that can be found in the COPYING file in the root of the source 5 | // tree. An additional intellectual property rights grant can be found 6 | // in the file PATENTS. All contributing project authors may 7 | // be found in the AUTHORS file in the root of the source tree. 8 | // ----------------------------------------------------------------------------- 9 | // 10 | // Common types 11 | // 12 | // Author: Skal (pascal.massimino@gmail.com) 13 | 14 | #ifndef WEBP_WEBP_TYPES_H_ 15 | #define WEBP_WEBP_TYPES_H_ 16 | 17 | #include // for size_t 18 | 19 | #ifndef _MSC_VER 20 | #include 21 | #ifdef __STRICT_ANSI__ 22 | #define WEBP_INLINE 23 | #else /* __STRICT_ANSI__ */ 24 | #define WEBP_INLINE inline 25 | #endif 26 | #else 27 | typedef signed char int8_t; 28 | typedef unsigned char uint8_t; 29 | typedef signed short int16_t; 30 | typedef unsigned short uint16_t; 31 | typedef signed int int32_t; 32 | typedef unsigned int uint32_t; 33 | typedef unsigned long long int uint64_t; 34 | typedef long long int int64_t; 35 | #define WEBP_INLINE __forceinline 36 | #endif /* _MSC_VER */ 37 | 38 | #ifndef WEBP_EXTERN 39 | // This explicitly marks library functions and allows for changing the 40 | // signature for e.g., Windows DLL builds. 41 | #define WEBP_EXTERN(type) extern type 42 | #endif /* WEBP_EXTERN */ 43 | 44 | // Macro to check ABI compatibility (same major revision number) 45 | #define WEBP_ABI_IS_INCOMPATIBLE(a, b) (((a) >> 8) != ((b) >> 8)) 46 | 47 | #endif /* WEBP_WEBP_TYPES_H_ */ 48 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | *.swf 2 | *.stackdump 3 | *.o 4 | *.obj 5 | out 6 | *.iws 7 | Thumbs.db 8 | -------------------------------------------------------------------------------- /sample/sample.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /sample/src/MouseWheel.as: -------------------------------------------------------------------------------- 1 | /** 2 | * MouseWheel by Denis Kolyako. Created January 16, 2009, last update: May 8, 2009 3 | * Visit http://etcs.ru for documentation, updates and more free code. 4 | * 5 | * You may distribute this class freely, provided it is not modified in any way (including 6 | * removing this header or changing the package path). 7 | * 8 | * 9 | * Please contact etc[at]mail.ru prior to distributing modified versions of this class. 10 | */ 11 | /** 12 | * The MouseWheel class lets you to capture mouse wheel on web page when your flash application in focus. 13 | * When wheel captured and your flash movie in focus, you cannot scroll page. 14 | * Works with IE/Firefox/Opera/Chrome on Windows. 15 | * Safari and any Mac OS versions of browsers are not supported :( 16 | */ 17 | package { 18 | 19 | import flash.display.Loader; 20 | import flash.errors.IllegalOperationError; 21 | import flash.events.AsyncErrorEvent; 22 | import flash.events.Event; 23 | import flash.events.StatusEvent; 24 | import flash.net.LocalConnection; 25 | import flash.utils.ByteArray; 26 | 27 | /** 28 | * @author etc 29 | * @version 1.1 30 | * @playerversion Flash 9 31 | * @langversion 3.0 32 | */ 33 | public final class MouseWheel { 34 | 35 | /** 36 | * @private 37 | */ 38 | private static const _bytes:ByteArray = new ByteArray(); 39 | 40 | /** 41 | * @private 42 | */ 43 | private static const _connection:LocalConnection = new LocalConnection(); 44 | 45 | /** 46 | * @private 47 | */ 48 | private static const _dataPart0:Array = new Array( 49 | 0x46575309, 50 | 0x28010000, 51 | 0x7800055F, 52 | 0x00000FA0, 53 | 0x00001F01, 54 | 0x00441100, 55 | 0x00000043, 56 | 0x02FFFFFF, 57 | 0x3F03FE00, 58 | 0x0000885C, 59 | 0x0009006C, 60 | 0x63004C6F, 61 | 0x63616C43, 62 | 0x6F6E6E65, 63 | 0x6374696F, 64 | 0x6E006C63 65 | ); 66 | 67 | /** 68 | * @private 69 | */ 70 | private static const _dataPart1:Array = new Array( 71 | 0x00636F6E, 72 | 0x6E656374, 73 | 0x00636170, 74 | 0x74757265, 75 | 0x004D6F75, 76 | 0x73650061, 77 | 0x64644C69, 78 | 0x7374656E, 79 | 0x65720072, 80 | 0x656D6F76, 81 | 0x654C6973, 82 | 0x74656E65, 83 | 0x72006F6E, 84 | 0x4D6F7573, 85 | 0x65576865, 86 | 0x656C0096, 87 | 0x0D000800, 88 | 0x06000000, 89 | 0x00000000, 90 | 0x00080140, 91 | 0x1D960900, 92 | 0x08020701, 93 | 0x00000008, 94 | 0x001C9602, 95 | 0x00080352, 96 | 0x17960200, 97 | 0x08001C96, 98 | 0x02000804, 99 | 0x8E0F0000, 100 | 0x01000329, 101 | 0x00027661, 102 | 0x6C756500, 103 | 0x38009602, 104 | 0x00040212, 105 | 0x9D020019, 106 | 0x00960900, 107 | 0x04010701, 108 | 0x00000008, 109 | 0x051C9602, 110 | 0x00080652, 111 | 0x17990200, 112 | 0x14009609, 113 | 0x00040107, 114 | 0x01000000, 115 | 0x08051C96, 116 | 0x02000807, 117 | 0x52174F96, 118 | 0x02000808, 119 | 0x9B050000, 120 | 0x00000000, 121 | 0x1D960900, 122 | 0x05010701, 123 | 0x00000008, 124 | 0x001C9602, 125 | 0x00080452, 126 | 0x17004000 127 | ); 128 | 129 | /** 130 | * @private 131 | */ 132 | private static var _loader:Loader; 133 | 134 | /** 135 | * @private 136 | */ 137 | private static var _connectionID:uint; 138 | 139 | /** 140 | * @private 141 | */ 142 | private static var _captured:Boolean = false; 143 | 144 | /** 145 | * @private 146 | */ 147 | private static var _complete:Boolean = false; 148 | 149 | /** 150 | * Returns current mouse wheel status (captured/released) 151 | */ 152 | public static function get captured():Boolean { 153 | return MouseWheel._captured; 154 | } 155 | 156 | /** 157 | * Capture mouse wheel on web page. 158 | */ 159 | public static function capture():void { 160 | if (MouseWheel._captured) return; 161 | 162 | if (!MouseWheel._bytes.length) { 163 | var rand:uint = Math.round(Math.random() * 100000); 164 | while (rand < 10000) rand = Math.round(Math.random() * 100000); 165 | MouseWheel._connectionID = rand; 166 | var bytes:ByteArray = MouseWheel._bytes; 167 | var data:Array = MouseWheel._dataPart0; 168 | var length:uint = data.length; 169 | var i:uint; 170 | 171 | for (i = 0;i < length;i++) { 172 | bytes.writeUnsignedInt(data[i]); 173 | } 174 | 175 | bytes.writeUTFBytes('_' + MouseWheel._connectionID.toString()); 176 | data = MouseWheel._dataPart1; 177 | length = data.length; 178 | 179 | for (i = 0;i < length;i++) { 180 | bytes.writeUnsignedInt(data[i]); 181 | } 182 | 183 | bytes.writeShort(0x0000); 184 | } 185 | 186 | MouseWheel._connection.addEventListener(StatusEvent.STATUS, MouseWheel.handler_status); 187 | MouseWheel._connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, MouseWheel.handler_asyncError); 188 | MouseWheel._captured = true; 189 | 190 | if (MouseWheel._loader) { 191 | MouseWheel.send(); 192 | } else { 193 | MouseWheel._loader = new Loader(); 194 | MouseWheel._loader.loadBytes(MouseWheel._bytes); 195 | MouseWheel._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, MouseWheel.handler_complete); 196 | } 197 | } 198 | 199 | /** 200 | * Release mouse wheel on web page. 201 | */ 202 | public static function release():void { 203 | if (!MouseWheel._captured) return; 204 | MouseWheel._captured = false; 205 | MouseWheel.send(); 206 | } 207 | 208 | /** 209 | * @private 210 | */ 211 | private static function send(event:Event = null):void { 212 | if (!MouseWheel._complete) return; 213 | MouseWheel._connection.send('lc_' + MouseWheel._connectionID, 'capture', MouseWheel._captured); 214 | } 215 | 216 | /** 217 | * @private 218 | */ 219 | private static function handler_complete(event:Event):void { 220 | MouseWheel._complete = true; 221 | if (!MouseWheel._captured) MouseWheel.send(); 222 | } 223 | 224 | /** 225 | * @private 226 | */ 227 | private static function handler_status(event:StatusEvent):void { 228 | if (event.level == 'error') MouseWheel.send(); // try again 229 | } 230 | 231 | /** 232 | * @private 233 | */ 234 | private static function handler_asyncError(event:AsyncErrorEvent):void { 235 | // do nothing 236 | } 237 | 238 | public function MouseWheel() { 239 | throw new IllegalOperationError('do not instantiate MouseWheel'); 240 | } 241 | } 242 | } -------------------------------------------------------------------------------- /sample/src/sample.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/sample/src/sample.jpg -------------------------------------------------------------------------------- /sample/src/test.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/sample/src/test.webp -------------------------------------------------------------------------------- /sample/src/test3.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/sample/src/test3.webp -------------------------------------------------------------------------------- /sample/src/texture.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soywiz-archive/as3libwebp/3b9e9c76c2990b63d15626050c5d0b27c20079f6/sample/src/texture.webp -------------------------------------------------------------------------------- /webp_extension.c: -------------------------------------------------------------------------------- 1 | //#include 2 | //BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { return TRUE; } 3 | 4 | #include 5 | #include 6 | #include 7 | #include "libwebp/src/webp/decode.h" 8 | #include "libwebp/src/dsp/dsp.h" 9 | #include "FlashRuntimeExtensions.h" 10 | 11 | #define fprintf_file(filename, ...) { FILE *f = fopen(filename, "wb"); fprintf(f, __VA_ARGS__); fclose(f); } 12 | 13 | FREObject WebpGetVersionAne(FREContext ctx, void* functionData, uint32_t argc, FREObject argv[]) 14 | { 15 | FREObject result; 16 | FRENewObjectFromUint32(1, &result); 17 | return result; 18 | } 19 | 20 | FREObject newInt32(int value) { FREObject result; return (FRENewObjectFromInt32(value, &result) == FRE_OK) ? result : NULL; } 21 | FREObject newUInt32(unsigned int value) { FREObject result; return (FRENewObjectFromUint32(value, &result) == FRE_OK) ? result : NULL; } 22 | 23 | double getDouble(FREObject object, double _default) { double value = _default; FREGetObjectAsDouble(object, &value); return value; } 24 | 25 | FREObjectType getType(FREObject object) { 26 | FREObjectType _type = FRE_TYPE_NULL; 27 | FREGetObjectType(object, &_type); 28 | return _type; 29 | } 30 | 31 | FREObject WebpEncodeAne(FREContext ctx, void* functionData, uint32_t argc, FREObject argv[]) 32 | { 33 | FREBitmapData2 input = {0}; 34 | FREByteArray output = {0}; 35 | uint8_t* output_pointer = NULL; 36 | size_t output_size = 0; 37 | FREObject result = NULL; 38 | FREObject temp[4]; 39 | float quality = 50; 40 | int y, y2; 41 | int width = input.width; 42 | int height = input.height; 43 | int stride = input.lineStride32 * 4; 44 | uint32_t* from_pointer = (uint32_t*)input.bits32; 45 | uint32_t* temp_pointer = (uint32_t*)malloc(stride * height); 46 | 47 | if (argc < 2) return NULL; 48 | 49 | if (getType(argv[0]) != FRE_TYPE_BITMAPDATA) return NULL; 50 | quality = (float)getDouble(argv[1], 50); 51 | 52 | FREAcquireBitmapData2(argv[0], &input); 53 | for (y = 0; y < height; y++) { 54 | y2 = input.isInvertedY ? (height - y - 1) : y; 55 | memcpy(temp_pointer + input.lineStride32 * y, from_pointer + width * y2, stride); 56 | } 57 | 58 | output_size = WebPEncodeBGRA(temp_pointer, width, height, stride, *((int *)&quality), &output_pointer); 59 | 60 | free(temp_pointer); 61 | FREReleaseBitmapData(argv[0]); 62 | 63 | if (output_size == 0) goto cleanup; 64 | 65 | FRENewObject("flash.utils.ByteArray", 0, NULL, &result, NULL); 66 | FRESetObjectProperty(result, "length", newInt32(output_size), NULL); 67 | 68 | FREAcquireByteArray(result, &output); 69 | if (output.length == output_size) memcpy(output.bytes, output_pointer, output_size); 70 | FREReleaseByteArray(result); 71 | 72 | cleanup:; 73 | 74 | if (output_pointer != NULL) { free(output_pointer); output_pointer = NULL; } 75 | return result; 76 | } 77 | 78 | FREObject WebpDecodeAne(FREContext ctx, void* functionData, uint32_t argc, FREObject argv[]) 79 | { 80 | FREByteArray input; 81 | FREObject result; 82 | FREResult _result; 83 | FREObject bitmapDataObject; 84 | FREBitmapData2 bitmapData; 85 | FREObject thrownException; 86 | uint32_t* output_pointer = NULL; 87 | int width, height; 88 | FREObject widthObject, heightObject; 89 | FREObject callArgv[8]; 90 | int y, y2; 91 | FREObjectType _type = FRE_TYPE_NULL; 92 | 93 | if (argc < 1) return NULL; 94 | 95 | _type = FRE_TYPE_NULL; 96 | if (FREGetObjectType(argv[0], &_type) != FRE_OK) return NULL; 97 | if (_type != FRE_TYPE_BYTEARRAY) return NULL; 98 | 99 | if ((_result = FREAcquireByteArray(argv[0], &input)) != FRE_OK) goto cleanup; 100 | output_pointer = (uint32_t *)WebPDecodeBGRA((const uint8_t*)input.bytes, (size_t)input.length, &width, &height); 101 | if ((_result = FREReleaseByteArray(argv[0])) != FRE_OK) goto cleanup; 102 | if (output_pointer == NULL) goto cleanup; 103 | 104 | FRENewObjectFromInt32(width, &widthObject); 105 | FRENewObjectFromInt32(height, &heightObject); 106 | 107 | callArgv[0] = widthObject; 108 | callArgv[1] = heightObject; 109 | FRENewObjectFromBool(1, &callArgv[2]); 110 | FRENewObjectFromUint32((unsigned int)0xFFFFFFFF, &callArgv[3]); 111 | 112 | if ((_result = FRENewObject("flash.display.BitmapData", 4, callArgv, &bitmapDataObject, &thrownException)) != FRE_OK) goto cleanup; 113 | 114 | FREAcquireBitmapData2(bitmapDataObject, &bitmapData); 115 | for (y = 0; y < height; y++) { 116 | y2 = bitmapData.isInvertedY ? (height - y - 1) : y; 117 | memcpy(bitmapData.bits32 + bitmapData.lineStride32 * y, output_pointer + width * y2, bitmapData.lineStride32 * 4); 118 | } 119 | FREInvalidateBitmapDataRect(bitmapDataObject, 0, 0, width, height); 120 | FREReleaseBitmapData(bitmapDataObject); 121 | 122 | result = bitmapDataObject; 123 | 124 | cleanup:; 125 | if (output_pointer != NULL) { free(output_pointer); output_pointer = NULL; } 126 | 127 | return result; 128 | } 129 | 130 | #define FUNC_MAX_COUNT 64 131 | 132 | #define FUNC_INIT(functionData) \ 133 | { \ 134 | void* globalFuncData = functionData; \ 135 | FRENamedFunction* _functions = (FRENamedFunction*) malloc(sizeof(FRENamedFunction) * (FUNC_MAX_COUNT)); \ 136 | *numFunctions = 0; 137 | 138 | #define FUNC_ADD(functionName, functionRef) \ 139 | _functions[*numFunctions].name = (const uint8_t*) functionName; \ 140 | _functions[*numFunctions].functionData = NULL; \ 141 | _functions[*numFunctions].function = &functionRef; \ 142 | (*numFunctions)++; 143 | 144 | #define FUNC_ADD2(functionRef) FUNC_ADD(#functionRef, functionRef) 145 | 146 | #define FUNC_END() \ 147 | *functions = _functions; \ 148 | } 149 | 150 | void contextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctions, const FRENamedFunction** functions) 151 | { 152 | FUNC_INIT(NULL); 153 | FUNC_ADD2(WebpDecodeAne); 154 | FUNC_ADD2(WebpEncodeAne); 155 | FUNC_ADD2(WebpGetVersionAne); 156 | FUNC_END(); 157 | //fprintf_file("c:/temp/1/contextInitializer", "numFunctions:%d\n", *numFunctions); 158 | } 159 | 160 | void contextFinalizer(FREContext ctx) 161 | { 162 | } 163 | 164 | #ifdef _WINDLL 165 | #define DLL_EXPORT __declspec(dllexport) 166 | #else 167 | #define DLL_EXPORT 168 | #endif 169 | DLL_EXPORT void initializer(void** extData, FREContextInitializer* ctxInitializer, FREContextFinalizer* ctxFinalizer) 170 | { 171 | *ctxInitializer = &contextInitializer; 172 | *ctxFinalizer = &contextFinalizer; 173 | } 174 | 175 | DLL_EXPORT void finalizer(void* extData) 176 | { 177 | } 178 | --------------------------------------------------------------------------------
69 | To view this page ensure that Adobe Flash Player version 70 | 0.0.0 or greater is installed. 71 |
95 | Either scripts and active content are not permitted to run or Adobe Flash Player version 96 | 0.0.0 or greater is not installed. 97 |