Repository: google/guetzli Branch: master Commit: 214f2bb42abf Files: 84 Total size: 454.5 KB Directory structure: gitextract_kvasvos7/ ├── .gitignore ├── .travis.sh ├── .travis.yml ├── BUILD ├── CONTRIBUTING.md ├── LICENSE ├── Makefile ├── README.md ├── WORKSPACE ├── appveyor.yml ├── external/ │ ├── png.BUILD │ └── zlib.BUILD ├── fuzz_target.cc ├── guetzli/ │ ├── butteraugli_comparator.cc │ ├── butteraugli_comparator.h │ ├── color_transform.h │ ├── comparator.h │ ├── dct_double.cc │ ├── dct_double.h │ ├── debug_print.cc │ ├── debug_print.h │ ├── entropy_encode.cc │ ├── entropy_encode.h │ ├── fast_log.h │ ├── fdct.cc │ ├── fdct.h │ ├── gamma_correct.cc │ ├── gamma_correct.h │ ├── guetzli.cc │ ├── idct.cc │ ├── idct.h │ ├── jpeg_bit_writer.h │ ├── jpeg_data.cc │ ├── jpeg_data.h │ ├── jpeg_data_decoder.cc │ ├── jpeg_data_decoder.h │ ├── jpeg_data_encoder.cc │ ├── jpeg_data_encoder.h │ ├── jpeg_data_reader.cc │ ├── jpeg_data_reader.h │ ├── jpeg_data_writer.cc │ ├── jpeg_data_writer.h │ ├── jpeg_error.h │ ├── jpeg_huffman_decode.cc │ ├── jpeg_huffman_decode.h │ ├── order.inc │ ├── output_image.cc │ ├── output_image.h │ ├── preprocess_downsample.cc │ ├── preprocess_downsample.h │ ├── processor.cc │ ├── processor.h │ ├── quality.cc │ ├── quality.h │ ├── quantize.cc │ ├── quantize.h │ ├── score.cc │ ├── score.h │ └── stats.h ├── guetzli.make ├── guetzli.sln ├── guetzli.vcxproj ├── guetzli.vcxproj.filters ├── guetzli_static.make ├── guetzli_static.vcxproj ├── guetzli_static.vcxproj.filters ├── premake5.lua ├── snapcraft.yaml ├── tests/ │ ├── golden_checksums.txt │ ├── golden_test.sh │ ├── png_checksums.txt │ └── smoke_test.sh ├── third_party/ │ └── butteraugli/ │ ├── BUILD │ ├── LICENSE │ ├── README.md │ ├── WORKSPACE │ ├── butteraugli/ │ │ ├── Makefile │ │ ├── butteraugli.cc │ │ ├── butteraugli.h │ │ └── butteraugli_main.cc │ ├── jpeg.BUILD │ ├── png.BUILD │ └── zlib.BUILD └── tools/ └── guetzli-compare.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ bin/ obj/ /bazel-* # Visual Studio 2015 cache/options directory .vs/ # Visual C++ cache files ipch/ *.aps *.ncb *.opendb *.opensdf *.sdf *.cachefile *.VC.db *.VC.VC.opendb ================================================ FILE: .travis.sh ================================================ #!/bin/bash case "$1" in "install") case "${TRAVIS_OS_NAME}" in "linux") ;; "osx") brew update brew install netpbm ;; esac case "${BUILD_SYSTEM}" in "bazel") case "${TRAVIS_OS_NAME}" in "linux") sudo apt-get remove openjdk-9-jdk oracle-java9-installer # Conflicts with Bazel. wget https://github.com/bazelbuild/bazel/releases/download/1.1.0/bazel_1.1.0-linux-x86_64.deb echo '138b47ffd54924e3c0264c65d31d3927803fb9025db4d5b18107df79ee3bda95 bazel_1.1.0-linux-x86_64.deb' | sha256sum -c --strict || exit 1 sudo dpkg -i bazel_1.1.0-linux-x86_64.deb ;; "osx") brew cask install homebrew/cask-versions/adoptopenjdk8 brew install bazel ;; esac ;; "make") ;; esac ;; "script") case "${BUILD_SYSTEM}" in "bazel") bazel build -c opt ...:all GUETZLI_BIN=bazel-bin/guetzli ;; "make") make GUETZLI_BIN=bin/Release/guetzli ;; esac tests/smoke_test.sh $GUETZLI_BIN ;; esac ================================================ FILE: .travis.yml ================================================ language: cpp sudo: false matrix: include: - os: linux dist: trusty sudo: required env: BUILD_SYSTEM=bazel addons: apt: sources: - ubuntu-toolchain-r-test packages: - libjpeg-progs - netpbm # For pngtopnm in smoke_test.sh. - wget - os: osx env: BUILD_SYSTEM=bazel - os: linux dist: trusty env: BUILD_SYSTEM=make addons: apt: sources: - ubuntu-toolchain-r-test packages: - libjpeg-progs - libpng-dev - netpbm # For pngtopnm in smoke_test.sh. - pkg-config - wget - os: osx env: BUILD_SYSTEM=make install: - ./.travis.sh install script: - ./.travis.sh script ================================================ FILE: BUILD ================================================ # Description: # Bazel build file for Guetzli. cc_library( name = "guetzli_lib", srcs = glob( [ "guetzli/*.h", "guetzli/*.cc", "guetzli/*.inc", ], exclude = ["guetzli/guetzli.cc"], ), copts = [ "-Wno-sign-compare" ], deps = [ "@butteraugli//:butteraugli_lib", ], ) cc_binary( name = "guetzli", srcs = ["guetzli/guetzli.cc"], deps = [ ":guetzli_lib", "@png_archive//:png", ], ) ================================================ FILE: CONTRIBUTING.md ================================================ Want to contribute bugfixes or non-output-modifying changes (e.g. speedups)? Great! First, read this page (including the small print at the end). Want to contribute changes that modify the generated JPEG file? Talk to us first. ### Before you contribute Before we can use your code, you must sign the [Google Individual Contributor License Agreement](https://cla.developers.google.com/about/google-individual) (CLA), which you can do online. The CLA is necessary mainly because you own the copyright to your changes, even after your contribution becomes part of our codebase, so we need your permission to use and distribute your code. We also need to be sure of various other things—for instance that you'll tell us if you know that your code infringes on other people's patents. You don't have to sign the CLA until after you've submitted your code for review and a member has approved it, but you must do it before we can put your code into our codebase. Before you start working on a larger contribution, you should get in touch with us first through the issue tracker with your idea so that we can help out and possibly guide you. Coordinating up front makes it much easier to avoid frustration later on. ### Code reviews All submissions, including submissions by project members, require review. We use Github pull requests for this purpose. ### The small print Contributions made by corporations are covered by a different agreement than the one above, the [Software Grant and Corporate Contributor License Agreement](https://cla.developers.google.com/about/google-corporate). ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: Makefile ================================================ # GNU Make workspace makefile autogenerated by Premake ifndef config config=release endif ifndef verbose SILENT = @ endif ifeq ($(config),release) guetzli_static_config = release guetzli_config = release endif ifeq ($(config),debug) guetzli_static_config = debug guetzli_config = debug endif PROJECTS := guetzli_static guetzli .PHONY: all clean help $(PROJECTS) all: $(PROJECTS) guetzli_static: ifneq (,$(guetzli_static_config)) @echo "==== Building guetzli_static ($(guetzli_static_config)) ====" @${MAKE} --no-print-directory -C . -f guetzli_static.make config=$(guetzli_static_config) endif guetzli: ifneq (,$(guetzli_config)) @echo "==== Building guetzli ($(guetzli_config)) ====" @${MAKE} --no-print-directory -C . -f guetzli.make config=$(guetzli_config) endif clean: @${MAKE} --no-print-directory -C . -f guetzli_static.make clean @${MAKE} --no-print-directory -C . -f guetzli.make clean help: @echo "Usage: make [config=name] [target]" @echo "" @echo "CONFIGURATIONS:" @echo " release" @echo " debug" @echo "" @echo "TARGETS:" @echo " all (default)" @echo " clean" @echo " guetzli_static" @echo " guetzli" @echo "" @echo "For more information, see http://industriousone.com/premake/quick-start" ================================================ FILE: README.md ================================================

Guetzli

# Introduction Guetzli is a JPEG encoder that aims for excellent compression density at high visual quality. Guetzli-generated images are typically 20-30% smaller than images of equivalent quality generated by libjpeg. Guetzli generates only sequential (nonprogressive) JPEGs due to faster decompression speeds they offer. [![Build Status](https://travis-ci.org/google/guetzli.svg?branch=master)](https://travis-ci.org/google/guetzli) # Building ## On POSIX systems 1. Get a copy of the source code, either by cloning this repository, or by downloading an [archive](https://github.com/google/guetzli/archive/master.zip) and unpacking it. 2. Install [libpng](http://www.libpng.org/pub/png/libpng.html). If using your operating system package manager, install development versions of the packages if the distinction exists. * On Ubuntu, do `apt-get install libpng-dev`. * On Fedora, do `dnf install libpng-devel`. * On Arch Linux, do `pacman -S libpng`. * On Alpine Linux, do `apk add libpng-dev`. 3. Run `make` and expect the binary to be created in `bin/Release/guetzli`. ## On Windows 1. Get a copy of the source code, either by cloning this repository, or by downloading an [archive](https://github.com/google/guetzli/archive/master.zip) and unpacking it. 2. Install [Visual Studio 2015](https://www.visualstudio.com) and [vcpkg](https://github.com/Microsoft/vcpkg) 3. Install `libpng` using vcpkg: `.\vcpkg install libpng`. 4. Cause the installed packages to be available system-wide: `.\vcpkg integrate install`. If you prefer not to do this, refer to [vcpkg's documentation](https://github.com/Microsoft/vcpkg/blob/master/docs/EXAMPLES.md#example-1-2). 5. Open the Visual Studio project enclosed in the repository and build it. ## On macOS To install using [Homebrew](https://brew.sh/): 1. Install [Homebrew](https://brew.sh/) 2. `brew install guetzli` To install using the repository: 1. Get a copy of the source code, either by cloning this repository, or by downloading an [archive](https://github.com/google/guetzli/archive/master.zip) and unpacking it. 2. Install [Homebrew](https://brew.sh/) or [MacPorts](https://www.macports.org/) 3. Install `libpng` * Using [Homebrew](https://brew.sh/): `brew install libpng`. * Using [MacPorts](https://www.macports.org/): `port install libpng` (You may need to use `sudo`). 4. Run the following command to build the binary in `bin/Release/guetzli`. * If you installed using [Homebrew](https://brew.sh/) simply use `make` * If you installed using [MacPorts](https://www.macports.org/) use `CFLAGS='-I/opt/local/include' LDFLAGS='-L/opt/local/lib' make` ## With Bazel There's also a [Bazel](https://bazel.build) build configuration provided. If you have Bazel installed, you can also compile Guetzli by running `bazel build -c opt //:guetzli`. # Using **Note:** Guetzli uses a large amount of memory. You should provide 300MB of memory per 1MPix of the input image. **Note:** Guetzli uses a significant amount of CPU time. You should count on using about 1 minute of CPU per 1 MPix of input image. **Note:** Guetzli assumes that input is in **sRGB profile** with a **gamma of 2.2**. Guetzli will ignore any color-profile metadata in the image. To try out Guetzli you need to [build](#building) or [download](https://github.com/google/guetzli/releases) the Guetzli binary. The binary reads a PNG or JPEG image and creates an optimized JPEG image: ```bash guetzli [--quality Q] [--verbose] original.png output.jpg guetzli [--quality Q] [--verbose] original.jpg output.jpg ``` Note that Guetzli is designed to work on high quality images. You should always prefer providing uncompressed input images (e.g. that haven't been already compressed with any JPEG encoders, including Guetzli). While it will work on other images too, results will be poorer. You can try compressing an enclosed [sample high quality image](https://github.com/google/guetzli/releases/download/v0/bees.png). You can pass a `--quality Q` parameter to set quality in units equivalent to libjpeg quality. You can also pass a `--verbose` flag to see a trace of encoding attempts made. Please note that JPEG images do not support alpha channel (transparency). If the input is a PNG with an alpha channel, it will be overlaid on black background before encoding. ================================================ FILE: WORKSPACE ================================================ # Description: # Bazel workspace file for Guetzli. workspace(name = "guetzli") load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "png_archive", build_file = "png.BUILD", sha256 = "a941dc09ca00148fe7aaf4ecdd6a67579c293678ed1e1cf633b5ffc02f4f8cf7", strip_prefix = "libpng-1.2.57", url = "http://github.com/glennrp/libpng/archive/v1.2.57.zip", ) http_archive( name = "zlib_archive", build_file = "zlib.BUILD", sha256 = "8d7e9f698ce48787b6e1c67e6bff79e487303e66077e25cb9784ac8835978017", strip_prefix = "zlib-1.2.10", url = "http://zlib.net/fossils/zlib-1.2.10.tar.gz", ) local_repository( name = "butteraugli", path = "third_party/butteraugli/", ) ================================================ FILE: appveyor.yml ================================================ version: '1.0.1#{build}' shallow_clone: true os: - Visual Studio 2015 environment: matrix: - TOOLSET: vs2015 install: - ps: Start-FileDownload 'https://github.com/premake/premake-core/releases/download/v5.0.0-alpha11/premake-5.0.0-alpha11-windows.zip' 'premake.zip' - 7z x premake.zip - premake5.exe %TOOLSET% - git clone https://github.com/Microsoft/vcpkg - md vcpkg\downloads\nuget-3.5.0 - appveyor DownloadFile https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -FileName %appveyor_build_folder%\vcpkg\downloads\nuget-3.5.0\nuget.exe - appveyor DownloadFile https://cmake.org/files/v3.8/cmake-3.8.0-rc1-win32-x86.zip -FileName %appveyor_build_folder%\vcpkg\downloads\cmake-3.8.0-rc1-win32-x86.zip - 7z x %appveyor_build_folder%\vcpkg\downloads\cmake-3.8.0-rc1-win32-x86.zip - cd vcpkg - powershell -exec bypass -File scripts\bootstrap.ps1 - vcpkg integrate install - vcpkg install libpng - cd .. configuration: - Debug - Release build: project: guetzli.sln ================================================ FILE: external/png.BUILD ================================================ # Description: # libpng is the official PNG reference library. licenses(["notice"]) # BSD/MIT-like license cc_library( name = "png", srcs = [ "png.c", "pngerror.c", "pngget.c", "pngmem.c", "pngpread.c", "pngread.c", "pngrio.c", "pngrtran.c", "pngrutil.c", "pngset.c", "pngtrans.c", "pngwio.c", "pngwrite.c", "pngwtran.c", "pngwutil.c", ], hdrs = [ "png.h", "pngconf.h", ], includes = ["."], linkopts = ["-lm"], visibility = ["//visibility:public"], deps = ["@zlib_archive//:zlib"], ) ================================================ FILE: external/zlib.BUILD ================================================ package(default_visibility = ["//visibility:public"]) licenses(["notice"]) # BSD/MIT-like license (for zlib) cc_library( name = "zlib", srcs = [ "adler32.c", "compress.c", "crc32.c", "crc32.h", "deflate.c", "deflate.h", "gzclose.c", "gzguts.h", "gzlib.c", "gzread.c", "gzwrite.c", "infback.c", "inffast.c", "inffast.h", "inffixed.h", "inflate.c", "inflate.h", "inftrees.c", "inftrees.h", "trees.c", "trees.h", "uncompr.c", "zconf.h", "zutil.c", "zutil.h", ], hdrs = ["zlib.h"], includes = ["."], ) ================================================ FILE: fuzz_target.cc ================================================ #include #include "guetzli/jpeg_data.h" #include "guetzli/jpeg_data_reader.h" #include "guetzli/processor.h" extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { std::string jpeg_data(reinterpret_cast(data), size); // Ignore large images, to prevent timeouts. guetzli::JPEGData jpg_header; if (!guetzli::ReadJpeg(data, size, guetzli::JPEG_READ_HEADER, &jpg_header)) { return 0; } static constexpr int kMaxPixels = 10000; if (static_cast(jpg_header.width) * jpg_header.height > kMaxPixels) { return 0; } // TODO(robryk): Use nondefault parameters. guetzli::Params params; std::string jpeg_out; (void)guetzli::Process(params, nullptr, jpeg_data, &jpeg_out); // TODO(robryk): Verify output distance if Process() succeeded. return 0; } ================================================ FILE: guetzli/butteraugli_comparator.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/butteraugli_comparator.h" #include #include "guetzli/debug_print.h" #include "guetzli/gamma_correct.h" #include "guetzli/score.h" namespace guetzli { namespace { using ::butteraugli::ImageF; using ::butteraugli::CreatePlanes; using ::butteraugli::PlanesFromPacked; using ::butteraugli::PackedFromPlanes; std::vector LinearRgb(const size_t xsize, const size_t ysize, const std::vector& rgb) { const double* lut = Srgb8ToLinearTable(); std::vector planes = CreatePlanes(xsize, ysize, 3); for (int c = 0; c < 3; ++c) { for (size_t y = 0; y < ysize; ++y) { const uint8_t* const BUTTERAUGLI_RESTRICT row_in = &rgb[3 * xsize * y]; float* const BUTTERAUGLI_RESTRICT row_out = planes[c].Row(y); for (size_t x = 0; x < xsize; ++x) { row_out[x] = lut[row_in[3 * x + c]]; } } } return planes; } } // namespace ButteraugliComparator::ButteraugliComparator(const int width, const int height, const std::vector* rgb, const float target_distance, ProcessStats* stats) : width_(width), height_(height), target_distance_(target_distance), rgb_orig_(*rgb), comparator_(LinearRgb(width_, height_, *rgb)), distance_(0.0), stats_(stats) {} void ButteraugliComparator::Compare(const OutputImage& img) { std::vector rgb0 = ::butteraugli::OpsinDynamicsImage(LinearRgb(width_, height_, rgb_orig_)); std::vector > rgb(3, std::vector(width_ * height_)); img.ToLinearRGB(&rgb); const std::vector rgb_planes = PlanesFromPacked(width_, height_, rgb); std::vector(width_ * height_).swap(distmap_); ImageF distmap; comparator_.Diffmap(rgb_planes, distmap); CopyToPacked(distmap, &distmap_); distance_ = ::butteraugli::ButteraugliScoreFromDiffmap(distmap); GUETZLI_LOG(stats_, " BA[100.00%%] D[%6.4f]", distance_); } namespace { // To change this to n, add the relevant FFTn function and kFFTnMapIndexTable. constexpr size_t kBlockEdge = 8; constexpr size_t kBlockSize = kBlockEdge * kBlockEdge; constexpr size_t kBlockEdgeHalf = kBlockEdge / 2; constexpr size_t kBlockHalf = kBlockEdge * kBlockEdgeHalf; // Contrast sensitivity related weights. // Contrast sensitivity matrix is a model of the contrast sensitivity of the // human eye. The contrast sensitivity function, also known as contrast // sensitivity curve, is a basic psychovisual function and it roughly means // that highest frequences have less impact than middle and low frequences. // // The order of coefficients here is slightly confusing. // It is a creative order that benefits from the mirroring of the fft. static const double *GetContrastSensitivityMatrix() { static double csf8x8[kBlockHalf + kBlockEdgeHalf + 1] = { 0.0, // no 8x8 dc, enough 'dc' is calculated from low/mid freq images. 0.0, 0.0, 0.0, 0.3831134973, 0.676303603859, 1.1550451483, 8, 8, 0.692062533689, 0.847511538605, 0.498250875965, 0.36198671102, 0.308982169883, 0.1312701920435, 4.71274312228, 1.1550451483, 0.847511538605, 4.71274312228, 0.991205724152, 1.30229591239, 0.627264168628, 0.4, 0.1312701920435, 0.676303603859, 0.498250875965, 0.991205724152, 0.5, 0.3831134973, 0.349686450518, 0.627264168628, 0.308982169883, 0.3831134973, 0.36198671102, 1.30229591239, 0.3831134973, 0.323078800177, }; return &csf8x8[0]; } struct Complex { double real; double imag; }; inline double abssq(const Complex& c) { return c.real * c.real + c.imag * c.imag; } static void TransposeBlock(Complex data[kBlockSize]) { for (int i = 0; i < kBlockEdge; i++) { for (int j = 0; j < i; j++) { std::swap(data[kBlockEdge * i + j], data[kBlockEdge * j + i]); } } } // D. J. Bernstein's Fast Fourier Transform algorithm on 4 elements. inline void FFT4(Complex* a) { double t1, t2, t3, t4, t5, t6, t7, t8; t5 = a[2].real; t1 = a[0].real - t5; t7 = a[3].real; t5 += a[0].real; t3 = a[1].real - t7; t7 += a[1].real; t8 = t5 + t7; a[0].real = t8; t5 -= t7; a[1].real = t5; t6 = a[2].imag; t2 = a[0].imag - t6; t6 += a[0].imag; t5 = a[3].imag; a[2].imag = t2 + t3; t2 -= t3; a[3].imag = t2; t4 = a[1].imag - t5; a[3].real = t1 + t4; t1 -= t4; a[2].real = t1; t5 += a[1].imag; a[0].imag = t6 + t5; t6 -= t5; a[1].imag = t6; } const double kSqrtHalf = 0.70710678118654752440084436210484903; // D. J. Bernstein's Fast Fourier Transform algorithm on 8 elements. void FFT8(Complex* a) { double t1, t2, t3, t4, t5, t6, t7, t8; t7 = a[4].imag; t4 = a[0].imag - t7; t7 += a[0].imag; a[0].imag = t7; t8 = a[6].real; t5 = a[2].real - t8; t8 += a[2].real; a[2].real = t8; t7 = a[6].imag; a[6].imag = t4 - t5; t4 += t5; a[4].imag = t4; t6 = a[2].imag - t7; t7 += a[2].imag; a[2].imag = t7; t8 = a[4].real; t3 = a[0].real - t8; t8 += a[0].real; a[0].real = t8; a[4].real = t3 - t6; t3 += t6; a[6].real = t3; t7 = a[5].real; t3 = a[1].real - t7; t7 += a[1].real; a[1].real = t7; t8 = a[7].imag; t6 = a[3].imag - t8; t8 += a[3].imag; a[3].imag = t8; t1 = t3 - t6; t3 += t6; t7 = a[5].imag; t4 = a[1].imag - t7; t7 += a[1].imag; a[1].imag = t7; t8 = a[7].real; t5 = a[3].real - t8; t8 += a[3].real; a[3].real = t8; t2 = t4 - t5; t4 += t5; t6 = t1 - t4; t8 = kSqrtHalf; t6 *= t8; a[5].real = a[4].real - t6; t1 += t4; t1 *= t8; a[5].imag = a[4].imag - t1; t6 += a[4].real; a[4].real = t6; t1 += a[4].imag; a[4].imag = t1; t5 = t2 - t3; t5 *= t8; a[7].imag = a[6].imag - t5; t2 += t3; t2 *= t8; a[7].real = a[6].real - t2; t2 += a[6].real; a[6].real = t2; t5 += a[6].imag; a[6].imag = t5; FFT4(a); // Reorder to the correct output order. // TODO(szabadka): Modify the above computation so that this is not needed. Complex tmp = a[2]; a[2] = a[3]; a[3] = a[5]; a[5] = a[7]; a[7] = a[4]; a[4] = a[1]; a[1] = a[6]; a[6] = tmp; } // Same as FFT8, but all inputs are real. // TODO(szabadka): Since this does not need to be in-place, maybe there is a // faster FFT than this one, which is derived from DJB's in-place complex FFT. void RealFFT8(const double* in, Complex* out) { double t1, t2, t3, t5, t6, t7, t8; t8 = in[6]; t5 = in[2] - t8; t8 += in[2]; out[2].real = t8; out[6].imag = -t5; out[4].imag = t5; t8 = in[4]; t3 = in[0] - t8; t8 += in[0]; out[0].real = t8; out[4].real = t3; out[6].real = t3; t7 = in[5]; t3 = in[1] - t7; t7 += in[1]; out[1].real = t7; t8 = in[7]; t5 = in[3] - t8; t8 += in[3]; out[3].real = t8; t2 = -t5; t6 = t3 - t5; t8 = kSqrtHalf; t6 *= t8; out[5].real = out[4].real - t6; t1 = t3 + t5; t1 *= t8; out[5].imag = out[4].imag - t1; t6 += out[4].real; out[4].real = t6; t1 += out[4].imag; out[4].imag = t1; t5 = t2 - t3; t5 *= t8; out[7].imag = out[6].imag - t5; t2 += t3; t2 *= t8; out[7].real = out[6].real - t2; t2 += out[6].real; out[6].real = t2; t5 += out[6].imag; out[6].imag = t5; t5 = out[2].real; t1 = out[0].real - t5; t7 = out[3].real; t5 += out[0].real; t3 = out[1].real - t7; t7 += out[1].real; t8 = t5 + t7; out[0].real = t8; t5 -= t7; out[1].real = t5; out[2].imag = t3; out[3].imag = -t3; out[3].real = t1; out[2].real = t1; out[0].imag = 0; out[1].imag = 0; // Reorder to the correct output order. // TODO(szabadka): Modify the above computation so that this is not needed. Complex tmp = out[2]; out[2] = out[3]; out[3] = out[5]; out[5] = out[7]; out[7] = out[4]; out[4] = out[1]; out[1] = out[6]; out[6] = tmp; } // Fills in block[kBlockEdgeHalf..(kBlockHalf+kBlockEdgeHalf)], and leaves the // rest unmodified. void ButteraugliFFTSquared(double block[kBlockSize]) { double global_mul = 0.000064; Complex block_c[kBlockSize]; assert(kBlockEdge == 8); for (int y = 0; y < kBlockEdge; ++y) { RealFFT8(block + y * kBlockEdge, block_c + y * kBlockEdge); } TransposeBlock(block_c); double r0[kBlockEdge]; double r1[kBlockEdge]; for (int x = 0; x < kBlockEdge; ++x) { r0[x] = block_c[x].real; r1[x] = block_c[kBlockHalf + x].real; } RealFFT8(r0, block_c); RealFFT8(r1, block_c + kBlockHalf); for (int y = 1; y < kBlockEdgeHalf; ++y) { FFT8(block_c + y * kBlockEdge); } for (int i = kBlockEdgeHalf; i < kBlockHalf + kBlockEdgeHalf + 1; ++i) { block[i] = abssq(block_c[i]); block[i] *= global_mul; } } void ButteraugliBlockDiff(double xyb0[3 * kBlockSize], double xyb1[3 * kBlockSize], double* diff_xyb) { const double *csf8x8 = GetContrastSensitivityMatrix(); double avgdiff_xyb[3] = {0.0}; for (int i = 0; i < 3 * kBlockSize; ++i) { avgdiff_xyb[i / kBlockSize] += xyb0[i] - xyb1[i]; } for (int c = 0; c < 3; ++c) { double avgdiff = avgdiff_xyb[c] / kBlockSize; diff_xyb[c] += 4.0 * avgdiff * avgdiff; } double x_diff[kBlockSize]; double y_diff[kBlockSize]; double b_diff[kBlockSize]; for (int i = 0; i < kBlockSize; ++i) { x_diff[i] = (xyb0[i] - xyb1[i]); y_diff[i] = (xyb0[kBlockSize + i] - xyb1[kBlockSize + i]); b_diff[i] = (xyb0[2 * kBlockSize + i] - xyb1[2 * kBlockSize + i]); } ButteraugliFFTSquared(x_diff); ButteraugliFFTSquared(y_diff); ButteraugliFFTSquared(b_diff); for (size_t i = kBlockEdgeHalf; i < kBlockHalf + kBlockEdgeHalf + 1; ++i) { double d = csf8x8[i]; diff_xyb[0] += d * x_diff[i]; diff_xyb[1] += d * y_diff[i]; diff_xyb[2] += d * b_diff[i]; } } } // namespace void ButteraugliComparator::StartBlockComparisons() { std::vector dummy(3); std::vector rgb0 = ::butteraugli::OpsinDynamicsImage(LinearRgb(width_, height_, rgb_orig_)); ::butteraugli::Mask(rgb0, rgb0, &mask_xyz_, &dummy); } void ButteraugliComparator::FinishBlockComparisons() { mask_xyz_.clear(); } void ButteraugliComparator::SwitchBlock(int block_x, int block_y, int factor_x, int factor_y) { block_x_ = block_x; block_y_ = block_y; factor_x_ = factor_x; factor_y_ = factor_y; per_block_pregamma_.resize(factor_x_ * factor_y_); const double* lut = Srgb8ToLinearTable(); for (int off_y = 0, bx = 0; off_y < factor_y_; ++off_y) { for (int off_x = 0; off_x < factor_x_; ++off_x, ++bx) { per_block_pregamma_[bx].resize(3, std::vector(kDCTBlockSize)); int block_xx = block_x_ * factor_x_ + off_x; int block_yy = block_y_ * factor_y_ + off_y; for (int iy = 0, i = 0; iy < 8; ++iy) { for (int ix = 0; ix < 8; ++ix, ++i) { int x = std::min(8 * block_xx + ix, width_ - 1); int y = std::min(8 * block_yy + iy, height_ - 1); int px = y * width_ + x; for (int c = 0; c < 3; ++c) { per_block_pregamma_[bx][c][i] = lut[rgb_orig_[3 * px + c]]; } } } per_block_pregamma_[bx] = ::butteraugli::PackedFromPlanes(::butteraugli::OpsinDynamicsImage( ::butteraugli::PlanesFromPacked(8, 8, per_block_pregamma_[bx]))); } } } double ButteraugliComparator::CompareBlock(const OutputImage& img, int off_x, int off_y) const { int block_x = block_x_ * factor_x_ + off_x; int block_y = block_y_ * factor_y_ + off_y; int xmin = 8 * block_x; int ymin = 8 * block_y; int block_ix = off_y * factor_x_ + off_x; const std::vector >& rgb0 = per_block_pregamma_[block_ix]; std::vector > rgb1(3, std::vector(kDCTBlockSize)); img.ToLinearRGB(xmin, ymin, 8, 8, &rgb1); rgb1 = ::butteraugli::PackedFromPlanes(::butteraugli::OpsinDynamicsImage( ::butteraugli::PlanesFromPacked(8, 8, rgb1))); double b0[3 * kDCTBlockSize]; double b1[3 * kDCTBlockSize]; for (int c = 0; c < 3; ++c) { for (int ix = 0; ix < kDCTBlockSize; ++ix) { b0[c * kDCTBlockSize + ix] = rgb0[c][ix]; b1[c * kDCTBlockSize + ix] = rgb1[c][ix]; } } double diff_xyz[3] = { 0.0 }; ButteraugliBlockDiff(b0, b1, diff_xyz); double diff = 0.0; for (int c = 0; c < 3; ++c) { diff += diff_xyz[c] * mask_xyz_[c].Row(ymin)[xmin]; } return sqrt(diff); } float ButteraugliComparator::BlockErrorLimit() const { return target_distance_; } void ButteraugliComparator::ComputeBlockErrorAdjustmentWeights( int direction, int max_block_dist, double target_mul, int factor_x, int factor_y, const std::vector& distmap, std::vector* block_weight) { const double target_distance = target_distance_ * target_mul; const int sizex = 8 * factor_x; const int sizey = 8 * factor_y; const int block_width = (width_ + sizex - 1) / sizex; const int block_height = (height_ + sizey - 1) / sizey; std::vector max_dist_per_block(block_width * block_height); for (int block_y = 0; block_y < block_height; ++block_y) { for (int block_x = 0; block_x < block_width; ++block_x) { int block_ix = block_y * block_width + block_x; int x_max = std::min(width_, sizex * (block_x + 1)); int y_max = std::min(height_, sizey * (block_y + 1)); float max_dist = 0.0; for (int y = sizey * block_y; y < y_max; ++y) { for (int x = sizex * block_x; x < x_max; ++x) { max_dist = std::max(max_dist, distmap[y * width_ + x]); } } max_dist_per_block[block_ix] = max_dist; } } for (int block_y = 0; block_y < block_height; ++block_y) { for (int block_x = 0; block_x < block_width; ++block_x) { int block_ix = block_y * block_width + block_x; float max_local_dist = static_cast(target_distance); int x_min = std::max(0, block_x - max_block_dist); int y_min = std::max(0, block_y - max_block_dist); int x_max = std::min(block_width, block_x + 1 + max_block_dist); int y_max = std::min(block_height, block_y + 1 + max_block_dist); for (int y = y_min; y < y_max; ++y) { for (int x = x_min; x < x_max; ++x) { max_local_dist = std::max(max_local_dist, max_dist_per_block[y * block_width + x]); } } if (direction > 0) { if (max_dist_per_block[block_ix] <= target_distance && max_local_dist <= 1.1 * target_distance) { (*block_weight)[block_ix] = 1.0; } } else { constexpr double kLocalMaxWeight = 0.5; if (max_dist_per_block[block_ix] <= (1 - kLocalMaxWeight) * target_distance + kLocalMaxWeight * max_local_dist) { continue; } for (int y = y_min; y < y_max; ++y) { for (int x = x_min; x < x_max; ++x) { int d = std::max(std::abs(y - block_y), std::abs(x - block_x)); int ix = y * block_width + x; (*block_weight)[ix] = std::max( (*block_weight)[ix], 1.0f / (d + 1.0f)); } } } } } } double ButteraugliComparator::ScoreOutputSize(int size) const { return ScoreJPEG(distance_, size, target_distance_); } } // namespace guetzli ================================================ FILE: guetzli/butteraugli_comparator.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_BUTTERAUGLI_COMPARATOR_H_ #define GUETZLI_BUTTERAUGLI_COMPARATOR_H_ #include #include "butteraugli/butteraugli.h" #include "guetzli/comparator.h" #include "guetzli/jpeg_data.h" #include "guetzli/output_image.h" #include "guetzli/stats.h" namespace guetzli { constexpr int kButteraugliStep = 3; class ButteraugliComparator : public Comparator { public: ButteraugliComparator(const int width, const int height, const std::vector* rgb, const float target_distance, ProcessStats* stats); void Compare(const OutputImage& img) override; void StartBlockComparisons() override; void FinishBlockComparisons() override; void SwitchBlock(int block_x, int block_y, int factor_x, int factor_y) override; double CompareBlock(const OutputImage& img, int off_x, int off_y) const override; double ScoreOutputSize(int size) const override; bool DistanceOK(double target_mul) const override { return distance_ <= target_mul * target_distance_; } const std::vector distmap() const override { return distmap_; } float distmap_aggregate() const override { return distance_; } float BlockErrorLimit() const override; void ComputeBlockErrorAdjustmentWeights( int direction, int max_block_dist, double target_mul, int factor_x, int factor_y, const std::vector& distmap, std::vector* block_weight) override; private: const int width_; const int height_; const float target_distance_; const std::vector& rgb_orig_; int block_x_; int block_y_; int factor_x_; int factor_y_; std::vector<::butteraugli::ImageF> mask_xyz_; std::vector>> per_block_pregamma_; ::butteraugli::ButteraugliComparator comparator_; float distance_; std::vector distmap_; ProcessStats* stats_; }; } // namespace guetzli #endif // GUETZLI_BUTTERAUGLI_COMPARATOR_H_ ================================================ FILE: guetzli/color_transform.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_COLOR_TRANSFORM_H_ #define GUETZLI_COLOR_TRANSFORM_H_ namespace guetzli { static const int kCrToRedTable[256] = { -179, -178, -177, -175, -174, -172, -171, -170, -168, -167, -165, -164, -163, -161, -160, -158, -157, -156, -154, -153, -151, -150, -149, -147, -146, -144, -143, -142, -140, -139, -137, -136, -135, -133, -132, -130, -129, -128, -126, -125, -123, -122, -121, -119, -118, -116, -115, -114, -112, -111, -109, -108, -107, -105, -104, -102, -101, -100, -98, -97, -95, -94, -93, -91, -90, -88, -87, -86, -84, -83, -81, -80, -79, -77, -76, -74, -73, -72, -70, -69, -67, -66, -64, -63, -62, -60, -59, -57, -56, -55, -53, -52, -50, -49, -48, -46, -45, -43, -42, -41, -39, -38, -36, -35, -34, -32, -31, -29, -28, -27, -25, -24, -22, -21, -20, -18, -17, -15, -14, -13, -11, -10, -8, -7, -6, -4, -3, -1, 0, 1, 3, 4, 6, 7, 8, 10, 11, 13, 14, 15, 17, 18, 20, 21, 22, 24, 25, 27, 28, 29, 31, 32, 34, 35, 36, 38, 39, 41, 42, 43, 45, 46, 48, 49, 50, 52, 53, 55, 56, 57, 59, 60, 62, 63, 64, 66, 67, 69, 70, 72, 73, 74, 76, 77, 79, 80, 81, 83, 84, 86, 87, 88, 90, 91, 93, 94, 95, 97, 98, 100, 101, 102, 104, 105, 107, 108, 109, 111, 112, 114, 115, 116, 118, 119, 121, 122, 123, 125, 126, 128, 129, 130, 132, 133, 135, 136, 137, 139, 140, 142, 143, 144, 146, 147, 149, 150, 151, 153, 154, 156, 157, 158, 160, 161, 163, 164, 165, 167, 168, 170, 171, 172, 174, 175, 177, 178 }; static const int kCbToBlueTable[256] = { -227, -225, -223, -222, -220, -218, -216, -214, -213, -211, -209, -207, -206, -204, -202, -200, -198, -197, -195, -193, -191, -190, -188, -186, -184, -183, -181, -179, -177, -175, -174, -172, -170, -168, -167, -165, -163, -161, -159, -158, -156, -154, -152, -151, -149, -147, -145, -144, -142, -140, -138, -136, -135, -133, -131, -129, -128, -126, -124, -122, -120, -119, -117, -115, -113, -112, -110, -108, -106, -105, -103, -101, -99, -97, -96, -94, -92, -90, -89, -87, -85, -83, -82, -80, -78, -76, -74, -73, -71, -69, -67, -66, -64, -62, -60, -58, -57, -55, -53, -51, -50, -48, -46, -44, -43, -41, -39, -37, -35, -34, -32, -30, -28, -27, -25, -23, -21, -19, -18, -16, -14, -12, -11, -9, -7, -5, -4, -2, 0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 18, 19, 21, 23, 25, 27, 28, 30, 32, 34, 35, 37, 39, 41, 43, 44, 46, 48, 50, 51, 53, 55, 57, 58, 60, 62, 64, 66, 67, 69, 71, 73, 74, 76, 78, 80, 82, 83, 85, 87, 89, 90, 92, 94, 96, 97, 99, 101, 103, 105, 106, 108, 110, 112, 113, 115, 117, 119, 120, 122, 124, 126, 128, 129, 131, 133, 135, 136, 138, 140, 142, 144, 145, 147, 149, 151, 152, 154, 156, 158, 159, 161, 163, 165, 167, 168, 170, 172, 174, 175, 177, 179, 181, 183, 184, 186, 188, 190, 191, 193, 195, 197, 198, 200, 202, 204, 206, 207, 209, 211, 213, 214, 216, 218, 220, 222, 223, 225, }; static const int kCrToGreenTable[256] = { 5990656, 5943854, 5897052, 5850250, 5803448, 5756646, 5709844, 5663042, 5616240, 5569438, 5522636, 5475834, 5429032, 5382230, 5335428, 5288626, 5241824, 5195022, 5148220, 5101418, 5054616, 5007814, 4961012, 4914210, 4867408, 4820606, 4773804, 4727002, 4680200, 4633398, 4586596, 4539794, 4492992, 4446190, 4399388, 4352586, 4305784, 4258982, 4212180, 4165378, 4118576, 4071774, 4024972, 3978170, 3931368, 3884566, 3837764, 3790962, 3744160, 3697358, 3650556, 3603754, 3556952, 3510150, 3463348, 3416546, 3369744, 3322942, 3276140, 3229338, 3182536, 3135734, 3088932, 3042130, 2995328, 2948526, 2901724, 2854922, 2808120, 2761318, 2714516, 2667714, 2620912, 2574110, 2527308, 2480506, 2433704, 2386902, 2340100, 2293298, 2246496, 2199694, 2152892, 2106090, 2059288, 2012486, 1965684, 1918882, 1872080, 1825278, 1778476, 1731674, 1684872, 1638070, 1591268, 1544466, 1497664, 1450862, 1404060, 1357258, 1310456, 1263654, 1216852, 1170050, 1123248, 1076446, 1029644, 982842, 936040, 889238, 842436, 795634, 748832, 702030, 655228, 608426, 561624, 514822, 468020, 421218, 374416, 327614, 280812, 234010, 187208, 140406, 93604, 46802, 0, -46802, -93604, -140406, -187208, -234010, -280812, -327614, -374416, -421218, -468020, -514822, -561624, -608426, -655228, -702030, -748832, -795634, -842436, -889238, -936040, -982842, -1029644, -1076446, -1123248, -1170050, -1216852, -1263654, -1310456, -1357258, -1404060, -1450862, -1497664, -1544466, -1591268, -1638070, -1684872, -1731674, -1778476, -1825278, -1872080, -1918882, -1965684, -2012486, -2059288, -2106090, -2152892, -2199694, -2246496, -2293298, -2340100, -2386902, -2433704, -2480506, -2527308, -2574110, -2620912, -2667714, -2714516, -2761318, -2808120, -2854922, -2901724, -2948526, -2995328, -3042130, -3088932, -3135734, -3182536, -3229338, -3276140, -3322942, -3369744, -3416546, -3463348, -3510150, -3556952, -3603754, -3650556, -3697358, -3744160, -3790962, -3837764, -3884566, -3931368, -3978170, -4024972, -4071774, -4118576, -4165378, -4212180, -4258982, -4305784, -4352586, -4399388, -4446190, -4492992, -4539794, -4586596, -4633398, -4680200, -4727002, -4773804, -4820606, -4867408, -4914210, -4961012, -5007814, -5054616, -5101418, -5148220, -5195022, -5241824, -5288626, -5335428, -5382230, -5429032, -5475834, -5522636, -5569438, -5616240, -5663042, -5709844, -5756646, -5803448, -5850250, -5897052, -5943854, }; static const int kCbToGreenTable[256] = { 2919680, 2897126, 2874572, 2852018, 2829464, 2806910, 2784356, 2761802, 2739248, 2716694, 2694140, 2671586, 2649032, 2626478, 2603924, 2581370, 2558816, 2536262, 2513708, 2491154, 2468600, 2446046, 2423492, 2400938, 2378384, 2355830, 2333276, 2310722, 2288168, 2265614, 2243060, 2220506, 2197952, 2175398, 2152844, 2130290, 2107736, 2085182, 2062628, 2040074, 2017520, 1994966, 1972412, 1949858, 1927304, 1904750, 1882196, 1859642, 1837088, 1814534, 1791980, 1769426, 1746872, 1724318, 1701764, 1679210, 1656656, 1634102, 1611548, 1588994, 1566440, 1543886, 1521332, 1498778, 1476224, 1453670, 1431116, 1408562, 1386008, 1363454, 1340900, 1318346, 1295792, 1273238, 1250684, 1228130, 1205576, 1183022, 1160468, 1137914, 1115360, 1092806, 1070252, 1047698, 1025144, 1002590, 980036, 957482, 934928, 912374, 889820, 867266, 844712, 822158, 799604, 777050, 754496, 731942, 709388, 686834, 664280, 641726, 619172, 596618, 574064, 551510, 528956, 506402, 483848, 461294, 438740, 416186, 393632, 371078, 348524, 325970, 303416, 280862, 258308, 235754, 213200, 190646, 168092, 145538, 122984, 100430, 77876, 55322, 32768, 10214, -12340, -34894, -57448, -80002, -102556, -125110, -147664, -170218, -192772, -215326, -237880, -260434, -282988, -305542, -328096, -350650, -373204, -395758, -418312, -440866, -463420, -485974, -508528, -531082, -553636, -576190, -598744, -621298, -643852, -666406, -688960, -711514, -734068, -756622, -779176, -801730, -824284, -846838, -869392, -891946, -914500, -937054, -959608, -982162, -1004716, -1027270, -1049824, -1072378, -1094932, -1117486, -1140040, -1162594, -1185148, -1207702, -1230256, -1252810, -1275364, -1297918, -1320472, -1343026, -1365580, -1388134, -1410688, -1433242, -1455796, -1478350, -1500904, -1523458, -1546012, -1568566, -1591120, -1613674, -1636228, -1658782, -1681336, -1703890, -1726444, -1748998, -1771552, -1794106, -1816660, -1839214, -1861768, -1884322, -1906876, -1929430, -1951984, -1974538, -1997092, -2019646, -2042200, -2064754, -2087308, -2109862, -2132416, -2154970, -2177524, -2200078, -2222632, -2245186, -2267740, -2290294, -2312848, -2335402, -2357956, -2380510, -2403064, -2425618, -2448172, -2470726, -2493280, -2515834, -2538388, -2560942, -2583496, -2606050, -2628604, -2651158, -2673712, -2696266, -2718820, -2741374, -2763928, -2786482, -2809036, -2831590, }; static const uint8_t kRangeLimitLut[4 * 256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, }; static const uint8_t* kRangeLimit = kRangeLimitLut + 384; inline void ColorTransformYCbCrToRGB(uint8_t* pixel) { int y = pixel[0]; int cb = pixel[1]; int cr = pixel[2]; pixel[0] = kRangeLimit[y + kCrToRedTable[cr]]; pixel[1] = kRangeLimit[y + ((kCrToGreenTable[cr] + kCbToGreenTable[cb]) >> 16)]; pixel[2] = kRangeLimit[y + kCbToBlueTable[cb]]; } } // namespace guetzli #endif // GUETZLI_COLOR_TRANSFORM_H_ ================================================ FILE: guetzli/comparator.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_COMPARATOR_H_ #define GUETZLI_COMPARATOR_H_ #include #include "guetzli/output_image.h" #include "guetzli/stats.h" namespace guetzli { // Represents a baseline image, a comparison metric and an image acceptance // criteria based on this metric. class Comparator { public: Comparator() {} virtual ~Comparator() {} // Compares img with the baseline image and saves the resulting distance map // inside the object. The provided image must have the same dimensions as the // baseline image. virtual void Compare(const OutputImage& img) = 0; // Must be called before any CompareBlock() calls can be called. virtual void StartBlockComparisons() = 0; // No more CompareBlock() calls can be called after this. virtual void FinishBlockComparisons() = 0; // Sets the coordinates of the current macro-block for the purpose of // CompareBlock() calls. virtual void SwitchBlock(int block_x, int block_y, int factor_x, int factor_y) = 0; // Compares the 8x8 block with offsets (off_x, off_y) within the current // macro-block of the baseline image with the same block of img and returns // the resulting per-block distance. The interpretation of the returned // distance depends on the comparator used. virtual double CompareBlock(const OutputImage& img, int off_x, int off_y) const = 0; // Returns the combined score of the output image in the last Compare() call // (or the baseline image, if Compare() was not called yet), based on output // size and the similarity metric. virtual double ScoreOutputSize(int size) const = 0; // Returns true if the argument of the last Compare() call (or the baseline // image, if Compare() was not called yet) meets the image acceptance // criteria. The target_mul modifies the acceptance criteria used in this call // the following way: // = 1.0 : the original acceptance criteria is used, // < 1.0 : a more strict acceptance criteria is used, // > 1.0 : a less strict acceptance criteria is used. virtual bool DistanceOK(double target_mul) const = 0; // Returns the distance map between the baseline image and the image in the // last Compare() call (or the baseline image, if Compare() was not called // yet). // The dimensions of the distance map are the same as the baseline image. // The interpretation of the distance values depend on the comparator used. virtual const std::vector distmap() const = 0; // Returns an aggregate distance or similarity value between the baseline // image and the image in the last Compare() call (or the baseline image, if // Compare() was not called yet). // The interpretation of this aggregate value depends on the comparator used. virtual float distmap_aggregate() const = 0; // Returns a heuristic cutoff on block errors in the sense that we won't // consider distortions where a block error is greater than this. virtual float BlockErrorLimit() const = 0; // Given the search direction (+1 for upwards and -1 for downwards) and the // current distance map, fills in *block_weight image with the relative block // error adjustment weights. // The target_mul param has the same semantics as in DistanceOK(). // Note that this is essentially a static function in the sense that it does // not depend on the last Compare() call. virtual void ComputeBlockErrorAdjustmentWeights( int direction, int max_block_dist, double target_mul, int factor_x, int factor_y, const std::vector& distmap, std::vector* block_weight) = 0; }; } // namespace guetzli #endif // GUETZLI_COMPARATOR_H_ ================================================ FILE: guetzli/dct_double.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/dct_double.h" #include #include namespace guetzli { namespace { // kDCTMatrix[8*u+x] = 0.5*alpha(u)*cos((2*x+1)*u*M_PI/16), // where alpha(0) = 1/sqrt(2) and alpha(u) = 1 for u > 0. static const double kDCTMatrix[64] = { 0.3535533906, 0.3535533906, 0.3535533906, 0.3535533906, 0.3535533906, 0.3535533906, 0.3535533906, 0.3535533906, 0.4903926402, 0.4157348062, 0.2777851165, 0.0975451610, -0.0975451610, -0.2777851165, -0.4157348062, -0.4903926402, 0.4619397663, 0.1913417162, -0.1913417162, -0.4619397663, -0.4619397663, -0.1913417162, 0.1913417162, 0.4619397663, 0.4157348062, -0.0975451610, -0.4903926402, -0.2777851165, 0.2777851165, 0.4903926402, 0.0975451610, -0.4157348062, 0.3535533906, -0.3535533906, -0.3535533906, 0.3535533906, 0.3535533906, -0.3535533906, -0.3535533906, 0.3535533906, 0.2777851165, -0.4903926402, 0.0975451610, 0.4157348062, -0.4157348062, -0.0975451610, 0.4903926402, -0.2777851165, 0.1913417162, -0.4619397663, 0.4619397663, -0.1913417162, -0.1913417162, 0.4619397663, -0.4619397663, 0.1913417162, 0.0975451610, -0.2777851165, 0.4157348062, -0.4903926402, 0.4903926402, -0.4157348062, 0.2777851165, -0.0975451610, }; void DCT1d(const double* in, int stride, double* out) { for (int x = 0; x < 8; ++x) { out[x * stride] = 0.0; for (int u = 0; u < 8; ++u) { out[x * stride] += kDCTMatrix[8 * x + u] * in[u * stride]; } } } void IDCT1d(const double* in, int stride, double* out) { for (int x = 0; x < 8; ++x) { out[x * stride] = 0.0; for (int u = 0; u < 8; ++u) { out[x * stride] += kDCTMatrix[8 * u + x] * in[u * stride]; } } } typedef void (*Transform1d)(const double* in, int stride, double* out); void TransformBlock(double block[64], Transform1d f) { double tmp[64]; for (int x = 0; x < 8; ++x) { f(&block[x], 8, &tmp[x]); } for (int y = 0; y < 8; ++y) { f(&tmp[8 * y], 1, &block[8 * y]); } } } // namespace void ComputeBlockDCTDouble(double block[64]) { TransformBlock(block, DCT1d); } void ComputeBlockIDCTDouble(double block[64]) { TransformBlock(block, IDCT1d); } } // namespace guetzli ================================================ FILE: guetzli/dct_double.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_DCT_DOUBLE_H_ #define GUETZLI_DCT_DOUBLE_H_ namespace guetzli { // Performs in-place floating point 8x8 DCT on block[0..63]. // Note that the DCT used here is the DCT-2 with the first term multiplied by // 1/sqrt(2) and the result scaled by 1/2. void ComputeBlockDCTDouble(double block[64]); // Performs in-place floating point 8x8 inverse DCT on block[0..63]. void ComputeBlockIDCTDouble(double block[64]); } // namespace guetzli #endif // GUETZLI_DCT_DOUBLE_H_ ================================================ FILE: guetzli/debug_print.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/debug_print.h" namespace guetzli { void PrintDebug(ProcessStats* stats, std::string s) { if (stats->debug_output) { stats->debug_output->append(s); } if (stats->debug_output_file) { fprintf(stats->debug_output_file, "%s", s.c_str()); } } } // namespace guetzli ================================================ FILE: guetzli/debug_print.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_DEBUG_PRINT_H_ #define GUETZLI_DEBUG_PRINT_H_ #include "guetzli/stats.h" namespace guetzli { void PrintDebug(ProcessStats* stats, std::string s); } // namespace guetzli #define GUETZLI_LOG(stats, ...) \ do { \ char debug_string[1024]; \ int res = snprintf(debug_string, sizeof(debug_string), \ __VA_ARGS__); \ assert(res > 0 && "expected successful printing"); \ (void)res; \ debug_string[sizeof(debug_string) - 1] = '\0'; \ ::guetzli::PrintDebug( \ stats, std::string(debug_string)); \ } while (0) #define GUETZLI_LOG_QUANT(stats, q) \ for (int y = 0; y < 8; ++y) { \ for (int c = 0; c < 3; ++c) { \ for (int x = 0; x < 8; ++x) \ GUETZLI_LOG(stats, " %2d", (q)[c][8 * y + x]); \ GUETZLI_LOG(stats, " "); \ } \ GUETZLI_LOG(stats, "\n"); \ } #endif // GUETZLI_DEBUG_PRINT_H_ ================================================ FILE: guetzli/entropy_encode.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Entropy encoding (Huffman) utilities. #include "guetzli/entropy_encode.h" #include #include namespace guetzli { bool SetDepth(int p0, HuffmanTree *pool, uint8_t *depth, int max_depth) { int stack[17]; int level = 0; int p = p0; assert(max_depth <= 16); stack[0] = -1; while (true) { if (pool[p].index_left_ >= 0) { level++; if (level > max_depth) return false; stack[level] = pool[p].index_right_or_value_; p = pool[p].index_left_; continue; } else { depth[pool[p].index_right_or_value_] = static_cast(level); } while (level >= 0 && stack[level] == -1) level--; if (level < 0) return true; p = stack[level]; stack[level] = -1; } } // Sort the root nodes, least popular first. static inline bool SortHuffmanTree(const HuffmanTree& v0, const HuffmanTree& v1) { if (v0.total_count_ != v1.total_count_) { return v0.total_count_ < v1.total_count_; } return v0.index_right_or_value_ > v1.index_right_or_value_; } // This function will create a Huffman tree. // // The catch here is that the tree cannot be arbitrarily deep. // Brotli specifies a maximum depth of 15 bits for "code trees" // and 7 bits for "code length code trees." // // count_limit is the value that is to be faked as the minimum value // and this minimum value is raised until the tree matches the // maximum length requirement. // // This algorithm is not of excellent performance for very long data blocks, // especially when population counts are longer than 2**tree_limit, but // we are not planning to use this with extremely long blocks. // // See http://en.wikipedia.org/wiki/Huffman_coding void CreateHuffmanTree(const uint32_t *data, const size_t length, const int tree_limit, HuffmanTree* tree, uint8_t *depth) { // For block sizes below 64 kB, we never need to do a second iteration // of this loop. Probably all of our block sizes will be smaller than // that, so this loop is mostly of academic interest. If we actually // would need this, we would be better off with the Katajainen algorithm. for (uint32_t count_limit = 1; ; count_limit *= 2) { size_t n = 0; for (size_t i = length; i != 0;) { --i; if (data[i]) { const uint32_t count = std::max(data[i], count_limit); tree[n++] = HuffmanTree(count, -1, static_cast(i)); } } if (n == 1) { depth[tree[0].index_right_or_value_] = 1; // Only one element. break; } std::sort(tree, tree + n, SortHuffmanTree); // The nodes are: // [0, n): the sorted leaf nodes that we start with. // [n]: we add a sentinel here. // [n + 1, 2n): new parent nodes are added here, starting from // (n+1). These are naturally in ascending order. // [2n]: we add a sentinel at the end as well. // There will be (2n+1) elements at the end. const HuffmanTree sentinel(~static_cast(0), -1, -1); tree[n] = sentinel; tree[n + 1] = sentinel; size_t i = 0; // Points to the next leaf node. size_t j = n + 1; // Points to the next non-leaf node. for (size_t k = n - 1; k != 0; --k) { size_t left, right; if (tree[i].total_count_ <= tree[j].total_count_) { left = i; ++i; } else { left = j; ++j; } if (tree[i].total_count_ <= tree[j].total_count_) { right = i; ++i; } else { right = j; ++j; } // The sentinel node becomes the parent node. size_t j_end = 2 * n - k; tree[j_end].total_count_ = tree[left].total_count_ + tree[right].total_count_; tree[j_end].index_left_ = static_cast(left); tree[j_end].index_right_or_value_ = static_cast(right); // Add back the last sentinel node. tree[j_end + 1] = sentinel; } if (SetDepth(static_cast(2 * n - 1), &tree[0], depth, tree_limit)) { /* We need to pack the Huffman tree in tree_limit bits. If this was not successful, add fake entities to the lowest values and retry. */ break; } } } } // namespace guetzli ================================================ FILE: guetzli/entropy_encode.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Entropy encoding (Huffman) utilities. #ifndef GUETZLI_ENTROPY_ENCODE_H_ #define GUETZLI_ENTROPY_ENCODE_H_ #include #include namespace guetzli { // A node of a Huffman tree. struct HuffmanTree { HuffmanTree() {} HuffmanTree(uint32_t count, int16_t left, int16_t right) : total_count_(count), index_left_(left), index_right_or_value_(right) { } uint32_t total_count_; int16_t index_left_; int16_t index_right_or_value_; }; bool SetDepth(int p, HuffmanTree *pool, uint8_t *depth, int max_depth); // This function will create a Huffman tree. // // The (data,length) contains the population counts. // The tree_limit is the maximum bit depth of the Huffman codes. // // The depth contains the tree, i.e., how many bits are used for // the symbol. // // The actual Huffman tree is constructed in the tree[] array, which has to // be at least 2 * length + 1 long. // // See http://en.wikipedia.org/wiki/Huffman_coding void CreateHuffmanTree(const uint32_t *data, const size_t length, const int tree_limit, HuffmanTree* tree, uint8_t *depth); } // namespace guetzli #endif // GUETZLI_ENTROPY_ENCODE_H_ ================================================ FILE: guetzli/fast_log.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_FAST_LOG_H_ #define GUETZLI_FAST_LOG_H_ #include namespace guetzli { inline int Log2FloorNonZero(uint32_t n) { #ifdef __GNUC__ return 31 ^ __builtin_clz(n); #else unsigned int result = 0; while (n >>= 1) result++; return result; #endif } inline int Log2Floor(uint32_t n) { return n == 0 ? -1 : Log2FloorNonZero(n); } } // namespace guetzli #endif // GUETZLI_FAST_LOG_H_ ================================================ FILE: guetzli/fdct.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Integer implementation of the Discrete Cosine Transform (DCT) // // Note! DCT output is kept scaled by 16, to retain maximum 16bit precision #include "guetzli/fdct.h" namespace guetzli { namespace { /////////////////////////////////////////////////////////////////////////////// // Cosine table: C(k) = cos(k.pi/16)/sqrt(2), k = 1..7 using 15 bits signed const coeff_t kTable04[7] = { 22725, 21407, 19266, 16384, 12873, 8867, 4520 }; // rows #1 and #7 are pre-multiplied by 2.C(1) before the 2nd pass. // This multiply is merged in the table of constants used during 1st pass: const coeff_t kTable17[7] = { 31521, 29692, 26722, 22725, 17855, 12299, 6270 }; // rows #2 and #6 are pre-multiplied by 2.C(2): const coeff_t kTable26[7] = { 29692, 27969, 25172, 21407, 16819, 11585, 5906 }; // rows #3 and #5 are pre-multiplied by 2.C(3): const coeff_t kTable35[7] = { 26722, 25172, 22654, 19266, 15137, 10426, 5315 }; /////////////////////////////////////////////////////////////////////////////// // Constants (15bit precision) and C macros for IDCT vertical pass #define kTan1 (13036) // = tan(pi/16) #define kTan2 (27146) // = tan(2.pi/16) = sqrt(2) - 1. #define kTan3m1 (-21746) // = tan(3.pi/16) - 1 #define k2Sqrt2 (23170) // = 1 / 2.sqrt(2) // performs: {a,b} <- {a-b, a+b}, without saturation #define BUTTERFLY(a, b) do { \ SUB((a), (b)); \ ADD((b), (b)); \ ADD((b), (a)); \ } while (0) /////////////////////////////////////////////////////////////////////////////// // Constants for DCT horizontal pass // Note about the CORRECT_LSB macro: // using 16bit fixed-point constants, we often compute products like: // p = (A*x + B*y + 32768) >> 16 by adding two sub-terms q = (A*x) >> 16 // and r = (B*y) >> 16 together. Statistically, we have p = q + r + 1 // in 3/4 of the cases. This can be easily seen from the relation: // (a + b + 1) >> 1 = (a >> 1) + (b >> 1) + ((a|b)&1) // The approximation we are doing is replacing ((a|b)&1) by 1. // In practice, this is a slightly more involved because the constants A and B // have also been rounded compared to their exact floating point value. // However, all in all the correction is quite small, and CORRECT_LSB can // be defined empty if needed. #define COLUMN_DCT8(in) do { \ LOAD(m0, (in)[0 * 8]); \ LOAD(m2, (in)[2 * 8]); \ LOAD(m7, (in)[7 * 8]); \ LOAD(m5, (in)[5 * 8]); \ \ BUTTERFLY(m0, m7); \ BUTTERFLY(m2, m5); \ \ LOAD(m3, (in)[3 * 8]); \ LOAD(m4, (in)[4 * 8]); \ BUTTERFLY(m3, m4); \ \ LOAD(m6, (in)[6 * 8]); \ LOAD(m1, (in)[1 * 8]); \ BUTTERFLY(m1, m6); \ BUTTERFLY(m7, m4); \ BUTTERFLY(m6, m5); \ \ /* RowIdct() needs 15bits fixed-point input, when the output from */ \ /* ColumnIdct() would be 12bits. We are better doing the shift by 3 */ \ /* now instead of in RowIdct(), because we have some multiplies to */ \ /* perform, that can take advantage of the extra 3bits precision. */ \ LSHIFT(m4, 3); \ LSHIFT(m5, 3); \ BUTTERFLY(m4, m5); \ STORE16((in)[0 * 8], m5); \ STORE16((in)[4 * 8], m4); \ \ LSHIFT(m7, 3); \ LSHIFT(m6, 3); \ LSHIFT(m3, 3); \ LSHIFT(m0, 3); \ \ LOAD_CST(m4, kTan2); \ m5 = m4; \ MULT(m4, m7); \ MULT(m5, m6); \ SUB(m4, m6); \ ADD(m5, m7); \ STORE16((in)[2 * 8], m5); \ STORE16((in)[6 * 8], m4); \ \ /* We should be multiplying m6 by C4 = 1/sqrt(2) here, but we only have */ \ /* the k2Sqrt2 = 1/(2.sqrt(2)) constant that fits into 15bits. So we */ \ /* shift by 4 instead of 3 to compensate for the additional 1/2 factor. */ \ LOAD_CST(m6, k2Sqrt2); \ LSHIFT(m2, 3 + 1); \ LSHIFT(m1, 3 + 1); \ BUTTERFLY(m1, m2); \ MULT(m2, m6); \ MULT(m1, m6); \ BUTTERFLY(m3, m1); \ BUTTERFLY(m0, m2); \ \ LOAD_CST(m4, kTan3m1); \ LOAD_CST(m5, kTan1); \ m7 = m3; \ m6 = m1; \ MULT(m3, m4); \ MULT(m1, m5); \ \ ADD(m3, m7); \ ADD(m1, m2); \ CORRECT_LSB(m1); \ CORRECT_LSB(m3); \ MULT(m4, m0); \ MULT(m5, m2); \ ADD(m4, m0); \ SUB(m0, m3); \ ADD(m7, m4); \ SUB(m5, m6); \ \ STORE16((in)[1 * 8], m1); \ STORE16((in)[3 * 8], m0); \ STORE16((in)[5 * 8], m7); \ STORE16((in)[7 * 8], m5); \ } while (0) // these are the macro required by COLUMN_* #define LOAD_CST(dst, src) (dst) = (src) #define LOAD(dst, src) (dst) = (src) #define MULT(a, b) (a) = (((a) * (b)) >> 16) #define ADD(a, b) (a) = (a) + (b) #define SUB(a, b) (a) = (a) - (b) #define LSHIFT(a, n) (a) = ((a) << (n)) #define STORE16(a, b) (a) = (b) #define CORRECT_LSB(a) (a) += 1 // DCT vertical pass inline void ColumnDct(coeff_t* in) { for (int i = 0; i < 8; ++i) { int m0, m1, m2, m3, m4, m5, m6, m7; COLUMN_DCT8(in + i); } } // DCT horizontal pass // We don't really need to round before descaling, since we // still have 4 bits of precision left as final scaled output. #define DESCALE(a) static_cast((a) >> 16) void RowDct(coeff_t* in, const coeff_t* table) { // The Fourier transform is an unitary operator, so we're basically // doing the transpose of RowIdct() const int a0 = in[0] + in[7]; const int b0 = in[0] - in[7]; const int a1 = in[1] + in[6]; const int b1 = in[1] - in[6]; const int a2 = in[2] + in[5]; const int b2 = in[2] - in[5]; const int a3 = in[3] + in[4]; const int b3 = in[3] - in[4]; // even part const int C2 = table[1]; const int C4 = table[3]; const int C6 = table[5]; const int c0 = a0 + a3; const int c1 = a0 - a3; const int c2 = a1 + a2; const int c3 = a1 - a2; in[0] = DESCALE(C4 * (c0 + c2)); in[4] = DESCALE(C4 * (c0 - c2)); in[2] = DESCALE(C2 * c1 + C6 * c3); in[6] = DESCALE(C6 * c1 - C2 * c3); // odd part const int C1 = table[0]; const int C3 = table[2]; const int C5 = table[4]; const int C7 = table[6]; in[1] = DESCALE(C1 * b0 + C3 * b1 + C5 * b2 + C7 * b3); in[3] = DESCALE(C3 * b0 - C7 * b1 - C1 * b2 - C5 * b3); in[5] = DESCALE(C5 * b0 - C1 * b1 + C7 * b2 + C3 * b3); in[7] = DESCALE(C7 * b0 - C5 * b1 + C3 * b2 - C1 * b3); } #undef DESCALE #undef LOAD_CST #undef LOAD #undef MULT #undef ADD #undef SUB #undef LSHIFT #undef STORE16 #undef CORRECT_LSB #undef kTan1 #undef kTan2 #undef kTan3m1 #undef k2Sqrt2 #undef BUTTERFLY #undef COLUMN_DCT8 } // namespace /////////////////////////////////////////////////////////////////////////////// // visible FDCT callable functions void ComputeBlockDCT(coeff_t* coeffs) { ColumnDct(coeffs); RowDct(coeffs + 0 * 8, kTable04); RowDct(coeffs + 1 * 8, kTable17); RowDct(coeffs + 2 * 8, kTable26); RowDct(coeffs + 3 * 8, kTable35); RowDct(coeffs + 4 * 8, kTable04); RowDct(coeffs + 5 * 8, kTable35); RowDct(coeffs + 6 * 8, kTable26); RowDct(coeffs + 7 * 8, kTable17); } } // namespace guetzli ================================================ FILE: guetzli/fdct.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_FDCT_H_ #define GUETZLI_FDCT_H_ #include "guetzli/jpeg_data.h" namespace guetzli { // Computes the DCT (Discrete Cosine Transform) of the 8x8 array in 'block', // scaled up by a factor of 16. The values in 'block' are laid out row-by-row // and the result is written to the same memory area. void ComputeBlockDCT(coeff_t* block); } // namespace guetzli #endif // GUETZLI_FDCT_H_ ================================================ FILE: guetzli/gamma_correct.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/gamma_correct.h" #include namespace guetzli { const double* NewSrgb8ToLinearTable() { double* table = new double[256]; int i = 0; for (; i < 11; ++i) { table[i] = i / 12.92; } for (; i < 256; ++i) { table[i] = 255.0 * std::pow(((i / 255.0) + 0.055) / 1.055, 2.4); } return table; } const double* Srgb8ToLinearTable() { static const double* const kSrgb8ToLinearTable = NewSrgb8ToLinearTable(); return kSrgb8ToLinearTable; } } // namespace guetzli ================================================ FILE: guetzli/gamma_correct.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_GAMMA_CORRECT_H_ #define GUETZLI_GAMMA_CORRECT_H_ namespace guetzli { const double* Srgb8ToLinearTable(); } // namespace guetzli #endif // GUETZLI_GAMMA_CORRECT_H_ ================================================ FILE: guetzli/guetzli.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include #include "png.h" #include "guetzli/jpeg_data.h" #include "guetzli/jpeg_data_reader.h" #include "guetzli/processor.h" #include "guetzli/quality.h" #include "guetzli/stats.h" namespace { constexpr int kDefaultJPEGQuality = 95; // An upper estimate of memory usage of Guetzli. The bound is // max(kLowerMemusaeMB * 1<<20, pixel_count * kBytesPerPixel) constexpr int kBytesPerPixel = 350; constexpr int kLowestMemusageMB = 100; // in MB constexpr int kDefaultMemlimitMB = 6000; // in MB inline uint8_t BlendOnBlack(const uint8_t val, const uint8_t alpha) { return (static_cast(val) * static_cast(alpha) + 128) / 255; } bool ReadPNG(const std::string& data, int* xsize, int* ysize, std::vector* rgb) { png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); if (!png_ptr) { return false; } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, nullptr, nullptr); return false; } if (setjmp(png_jmpbuf(png_ptr)) != 0) { // Ok we are here because of the setjmp. png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); return false; } std::istringstream memstream(data, std::ios::in | std::ios::binary); png_set_read_fn(png_ptr, static_cast(&memstream), [](png_structp png_ptr, png_bytep outBytes, png_size_t byteCountToRead) { std::istringstream& memstream = *static_cast(png_get_io_ptr(png_ptr)); memstream.read(reinterpret_cast(outBytes), byteCountToRead); if (memstream.eof()) png_error(png_ptr, "unexpected end of data"); if (memstream.fail()) png_error(png_ptr, "read from memory error"); }); // The png_transforms flags are as follows: // packing == convert 1,2,4 bit images, // strip == 16 -> 8 bits / channel, // shift == use sBIT dynamics, and // expand == palettes -> rgb, grayscale -> 8 bit images, tRNS -> alpha. const unsigned int png_transforms = PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_STRIP_16; png_read_png(png_ptr, info_ptr, png_transforms, nullptr); png_bytep* row_pointers = png_get_rows(png_ptr, info_ptr); *xsize = png_get_image_width(png_ptr, info_ptr); *ysize = png_get_image_height(png_ptr, info_ptr); rgb->resize(3 * (*xsize) * (*ysize)); const int components = png_get_channels(png_ptr, info_ptr); switch (components) { case 1: { // GRAYSCALE for (int y = 0; y < *ysize; ++y) { const uint8_t* row_in = row_pointers[y]; uint8_t* row_out = &(*rgb)[3 * y * (*xsize)]; for (int x = 0; x < *xsize; ++x) { const uint8_t gray = row_in[x]; row_out[3 * x + 0] = gray; row_out[3 * x + 1] = gray; row_out[3 * x + 2] = gray; } } break; } case 2: { // GRAYSCALE + ALPHA for (int y = 0; y < *ysize; ++y) { const uint8_t* row_in = row_pointers[y]; uint8_t* row_out = &(*rgb)[3 * y * (*xsize)]; for (int x = 0; x < *xsize; ++x) { const uint8_t gray = BlendOnBlack(row_in[2 * x], row_in[2 * x + 1]); row_out[3 * x + 0] = gray; row_out[3 * x + 1] = gray; row_out[3 * x + 2] = gray; } } break; } case 3: { // RGB for (int y = 0; y < *ysize; ++y) { const uint8_t* row_in = row_pointers[y]; uint8_t* row_out = &(*rgb)[3 * y * (*xsize)]; memcpy(row_out, row_in, 3 * (*xsize)); } break; } case 4: { // RGBA for (int y = 0; y < *ysize; ++y) { const uint8_t* row_in = row_pointers[y]; uint8_t* row_out = &(*rgb)[3 * y * (*xsize)]; for (int x = 0; x < *xsize; ++x) { const uint8_t alpha = row_in[4 * x + 3]; row_out[3 * x + 0] = BlendOnBlack(row_in[4 * x + 0], alpha); row_out[3 * x + 1] = BlendOnBlack(row_in[4 * x + 1], alpha); row_out[3 * x + 2] = BlendOnBlack(row_in[4 * x + 2], alpha); } } break; } default: png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); return false; } png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); return true; } std::string ReadFileOrDie(const char* filename) { bool read_from_stdin = strncmp(filename, "-", 2) == 0; FILE* f = read_from_stdin ? stdin : fopen(filename, "rb"); if (!f) { perror("Can't open input file"); exit(1); } std::string result; off_t buffer_size = 8192; if (fseek(f, 0, SEEK_END) == 0) { buffer_size = std::max(ftell(f), 1); if (fseek(f, 0, SEEK_SET) != 0) { perror("fseek"); exit(1); } } else if (ferror(f)) { perror("fseek"); exit(1); } std::unique_ptr buf(new char[buffer_size]); while (!feof(f)) { size_t read_bytes = fread(buf.get(), sizeof(char), buffer_size, f); if (ferror(f)) { perror("fread"); exit(1); } result.append(buf.get(), read_bytes); } fclose(f); return result; } void WriteFileOrDie(const char* filename, const std::string& contents) { bool write_to_stdout = strncmp(filename, "-", 2) == 0; FILE* f = write_to_stdout ? stdout : fopen(filename, "wb"); if (!f) { perror("Can't open output file for writing"); exit(1); } if (fwrite(contents.data(), 1, contents.size(), f) != contents.size()) { perror("fwrite"); exit(1); } if (fclose(f) < 0) { perror("fclose"); exit(1); } } void TerminateHandler() { fprintf(stderr, "Unhandled exception. Most likely insufficient memory available.\n" "Make sure that there is 300MB/MPix of memory available.\n"); exit(1); } void Usage() { fprintf(stderr, "Guetzli JPEG compressor. Usage: \n" "guetzli [flags] input_filename output_filename\n" "\n" "Flags:\n" " --verbose - Print a verbose trace of all attempts to standard output.\n" " --quality Q - Visual quality to aim for, expressed as a JPEG quality value.\n" " Default value is %d.\n" " --memlimit M - Memory limit in MB. Guetzli will fail if unable to stay under\n" " the limit. Default limit is %d MB.\n" " --nomemlimit - Do not limit memory usage.\n", kDefaultJPEGQuality, kDefaultMemlimitMB); exit(1); } } // namespace int main(int argc, char** argv) { std::set_terminate(TerminateHandler); int verbose = 0; int quality = kDefaultJPEGQuality; int memlimit_mb = kDefaultMemlimitMB; int opt_idx = 1; for(;opt_idx < argc;opt_idx++) { if (strnlen(argv[opt_idx], 2) < 2 || argv[opt_idx][0] != '-' || argv[opt_idx][1] != '-') break; if (!strcmp(argv[opt_idx], "--verbose")) { verbose = 1; } else if (!strcmp(argv[opt_idx], "--quality")) { opt_idx++; if (opt_idx >= argc) Usage(); quality = atoi(argv[opt_idx]); } else if (!strcmp(argv[opt_idx], "--memlimit")) { opt_idx++; if (opt_idx >= argc) Usage(); memlimit_mb = atoi(argv[opt_idx]); } else if (!strcmp(argv[opt_idx], "--nomemlimit")) { memlimit_mb = -1; } else if (!strcmp(argv[opt_idx], "--")) { opt_idx++; break; } else { fprintf(stderr, "Unknown commandline flag: %s\n", argv[opt_idx]); Usage(); } } if (argc - opt_idx != 2) { Usage(); } std::string in_data = ReadFileOrDie(argv[opt_idx]); std::string out_data; guetzli::Params params; params.butteraugli_target = static_cast( guetzli::ButteraugliScoreForQuality(quality)); guetzli::ProcessStats stats; if (verbose) { stats.debug_output_file = stderr; } static const unsigned char kPNGMagicBytes[] = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n', }; if (in_data.size() >= 8 && memcmp(in_data.data(), kPNGMagicBytes, sizeof(kPNGMagicBytes)) == 0) { int xsize, ysize; std::vector rgb; if (!ReadPNG(in_data, &xsize, &ysize, &rgb)) { fprintf(stderr, "Error reading PNG data from input file\n"); return 1; } double pixels = static_cast(xsize) * ysize; if (memlimit_mb != -1 && (pixels * kBytesPerPixel / (1 << 20) > memlimit_mb || memlimit_mb < kLowestMemusageMB)) { fprintf(stderr, "Memory limit would be exceeded. Failing.\n"); return 1; } if (!guetzli::Process(params, &stats, rgb, xsize, ysize, &out_data)) { fprintf(stderr, "Guetzli processing failed\n"); return 1; } } else { guetzli::JPEGData jpg_header; if (!guetzli::ReadJpeg(in_data, guetzli::JPEG_READ_HEADER, &jpg_header)) { fprintf(stderr, "Error reading JPG data from input file\n"); return 1; } double pixels = static_cast(jpg_header.width) * jpg_header.height; if (memlimit_mb != -1 && (pixels * kBytesPerPixel / (1 << 20) > memlimit_mb || memlimit_mb < kLowestMemusageMB)) { fprintf(stderr, "Memory limit would be exceeded. Failing.\n"); return 1; } if (!guetzli::Process(params, &stats, in_data, &out_data)) { fprintf(stderr, "Guetzli processing failed\n"); return 1; } } WriteFileOrDie(argv[opt_idx + 1], out_data); return 0; } ================================================ FILE: guetzli/idct.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Integer implementation of the Inverse Discrete Cosine Transform (IDCT). #include "guetzli/idct.h" #include #include namespace guetzli { // kIDCTMatrix[8*x+u] = alpha(u)*cos((2*x+1)*u*M_PI/16)*sqrt(2), with fixed 13 // bit precision, where alpha(0) = 1/sqrt(2) and alpha(u) = 1 for u > 0. // Some coefficients are off by +-1 to mimick libjpeg's behaviour. static const int kIDCTMatrix[kDCTBlockSize] = { 8192, 11363, 10703, 9633, 8192, 6437, 4433, 2260, 8192, 9633, 4433, -2259, -8192, -11362, -10704, -6436, 8192, 6437, -4433, -11362, -8192, 2261, 10704, 9633, 8192, 2260, -10703, -6436, 8192, 9633, -4433, -11363, 8192, -2260, -10703, 6436, 8192, -9633, -4433, 11363, 8192, -6437, -4433, 11362, -8192, -2261, 10704, -9633, 8192, -9633, 4433, 2259, -8192, 11362, -10704, 6436, 8192, -11363, 10703, -9633, 8192, -6437, 4433, -2260, }; // Computes out[x] = sum{kIDCTMatrix[8*x+u]*in[u*stride]; for u in [0..7]} inline void Compute1dIDCT(const coeff_t* in, const int stride, int out[8]) { int tmp0, tmp1, tmp2, tmp3, tmp4; tmp1 = kIDCTMatrix[0] * in[0]; out[0] = out[1] = out[2] = out[3] = out[4] = out[5] = out[6] = out[7] = tmp1; tmp0 = in[stride]; tmp1 = kIDCTMatrix[ 1] * tmp0; tmp2 = kIDCTMatrix[ 9] * tmp0; tmp3 = kIDCTMatrix[17] * tmp0; tmp4 = kIDCTMatrix[25] * tmp0; out[0] += tmp1; out[1] += tmp2; out[2] += tmp3; out[3] += tmp4; out[4] -= tmp4; out[5] -= tmp3; out[6] -= tmp2; out[7] -= tmp1; tmp0 = in[2 * stride]; tmp1 = kIDCTMatrix[ 2] * tmp0; tmp2 = kIDCTMatrix[10] * tmp0; out[0] += tmp1; out[1] += tmp2; out[2] -= tmp2; out[3] -= tmp1; out[4] -= tmp1; out[5] -= tmp2; out[6] += tmp2; out[7] += tmp1; tmp0 = in[3 * stride]; tmp1 = kIDCTMatrix[ 3] * tmp0; tmp2 = kIDCTMatrix[11] * tmp0; tmp3 = kIDCTMatrix[19] * tmp0; tmp4 = kIDCTMatrix[27] * tmp0; out[0] += tmp1; out[1] += tmp2; out[2] += tmp3; out[3] += tmp4; out[4] -= tmp4; out[5] -= tmp3; out[6] -= tmp2; out[7] -= tmp1; tmp0 = in[4 * stride]; tmp1 = kIDCTMatrix[ 4] * tmp0; out[0] += tmp1; out[1] -= tmp1; out[2] -= tmp1; out[3] += tmp1; out[4] += tmp1; out[5] -= tmp1; out[6] -= tmp1; out[7] += tmp1; tmp0 = in[5 * stride]; tmp1 = kIDCTMatrix[ 5] * tmp0; tmp2 = kIDCTMatrix[13] * tmp0; tmp3 = kIDCTMatrix[21] * tmp0; tmp4 = kIDCTMatrix[29] * tmp0; out[0] += tmp1; out[1] += tmp2; out[2] += tmp3; out[3] += tmp4; out[4] -= tmp4; out[5] -= tmp3; out[6] -= tmp2; out[7] -= tmp1; tmp0 = in[6 * stride]; tmp1 = kIDCTMatrix[ 6] * tmp0; tmp2 = kIDCTMatrix[14] * tmp0; out[0] += tmp1; out[1] += tmp2; out[2] -= tmp2; out[3] -= tmp1; out[4] -= tmp1; out[5] -= tmp2; out[6] += tmp2; out[7] += tmp1; tmp0 = in[7 * stride]; tmp1 = kIDCTMatrix[ 7] * tmp0; tmp2 = kIDCTMatrix[15] * tmp0; tmp3 = kIDCTMatrix[23] * tmp0; tmp4 = kIDCTMatrix[31] * tmp0; out[0] += tmp1; out[1] += tmp2; out[2] += tmp3; out[3] += tmp4; out[4] -= tmp4; out[5] -= tmp3; out[6] -= tmp2; out[7] -= tmp1; } void ComputeBlockIDCT(const coeff_t* block, uint8_t* out) { coeff_t colidcts[kDCTBlockSize]; const int kColScale = 11; const int kColRound = 1 << (kColScale - 1); for (int x = 0; x < 8; ++x) { int colbuf[8] = { 0 }; Compute1dIDCT(&block[x], 8, colbuf); for (int y = 0; y < 8; ++y) { colidcts[8 * y + x] = (colbuf[y] + kColRound) >> kColScale; } } const int kRowScale = 18; const int kRowRound = 257 << (kRowScale - 1); // includes offset by 128 for (int y = 0; y < 8; ++y) { const int rowidx = 8 * y; int rowbuf[8] = { 0 }; Compute1dIDCT(&colidcts[rowidx], 1, rowbuf); for (int x = 0; x < 8; ++x) { out[rowidx + x] = std::max(0, std::min(255, (rowbuf[x] + kRowRound) >> kRowScale)); } } } } // namespace guetzli ================================================ FILE: guetzli/idct.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_IDCT_H_ #define GUETZLI_IDCT_H_ #include "guetzli/jpeg_data.h" namespace guetzli { // Fills in 'result' with the inverse DCT of 'block'. // The arguments 'block' and 'result' point to 8x8 arrays that are arranged in // a row-by-row memory layout. void ComputeBlockIDCT(const coeff_t* block, uint8_t* result); } // namespace guetzli #endif // GUETZLI_IDCT_H_ ================================================ FILE: guetzli/jpeg_bit_writer.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_JPEG_BIT_WRITER_H_ #define GUETZLI_JPEG_BIT_WRITER_H_ #include #include namespace guetzli { // Returns non-zero if and only if x has a zero byte, i.e. one of // x & 0xff, x & 0xff00, ..., x & 0xff00000000000000 is zero. inline uint64_t HasZeroByte(uint64_t x) { return (x - 0x0101010101010101ULL) & ~x & 0x8080808080808080ULL; } // Handles the packing of bits into output bytes. struct BitWriter { explicit BitWriter(size_t length) : len(length), data(new uint8_t[len]), pos(0), put_buffer(0), put_bits(64), overflow(false) {} void WriteBits(int nbits, uint64_t bits) { put_bits -= nbits; put_buffer |= (bits << put_bits); if (put_bits <= 16) { // At this point we are ready to emit the most significant 6 bytes of // put_buffer_ to the output. // The JPEG format requires that after every 0xff byte in the entropy // coded section, there is a zero byte, therefore we first check if any of // the 6 most significant bytes of put_buffer_ is 0xff. if (HasZeroByte(~put_buffer | 0xffff)) { // We have a 0xff byte somewhere, examine each byte and append a zero // byte if necessary. EmitByte((put_buffer >> 56) & 0xff); EmitByte((put_buffer >> 48) & 0xff); EmitByte((put_buffer >> 40) & 0xff); EmitByte((put_buffer >> 32) & 0xff); EmitByte((put_buffer >> 24) & 0xff); EmitByte((put_buffer >> 16) & 0xff); } else if (pos + 6 < len) { // We don't have any 0xff bytes, output all 6 bytes without checking. data[pos] = (put_buffer >> 56) & 0xff; data[pos + 1] = (put_buffer >> 48) & 0xff; data[pos + 2] = (put_buffer >> 40) & 0xff; data[pos + 3] = (put_buffer >> 32) & 0xff; data[pos + 4] = (put_buffer >> 24) & 0xff; data[pos + 5] = (put_buffer >> 16) & 0xff; pos += 6; } else { overflow = true; } put_buffer <<= 48; put_bits += 48; } } // Writes the given byte to the output, writes an extra zero if byte is 0xff. void EmitByte(int byte) { if (pos < len) { data[pos++] = byte; } else { overflow = true; } if (byte == 0xff) { EmitByte(0); } } void JumpToByteBoundary() { while (put_bits <= 56) { int c = (put_buffer >> 56) & 0xff; EmitByte(c); put_buffer <<= 8; put_bits += 8; } if (put_bits < 64) { int padmask = 0xff >> (64 - put_bits); int c = ((put_buffer >> 56) & ~padmask) | padmask; EmitByte(c); } put_buffer = 0; put_bits = 64; } size_t len; std::unique_ptr data; size_t pos; uint64_t put_buffer; int put_bits; bool overflow; }; } // namespace guetzli #endif // GUETZLI_JPEG_BIT_WRITER_H_ ================================================ FILE: guetzli/jpeg_data.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/jpeg_data.h" #include #include namespace guetzli { bool JPEGData::Is420() const { return (components.size() == 3 && max_h_samp_factor == 2 && max_v_samp_factor == 2 && components[0].h_samp_factor == 2 && components[0].v_samp_factor == 2 && components[1].h_samp_factor == 1 && components[1].v_samp_factor == 1 && components[2].h_samp_factor == 1 && components[2].v_samp_factor == 1); } bool JPEGData::Is444() const { return (components.size() == 3 && max_h_samp_factor == 1 && max_v_samp_factor == 1 && components[0].h_samp_factor == 1 && components[0].v_samp_factor == 1 && components[1].h_samp_factor == 1 && components[1].v_samp_factor == 1 && components[2].h_samp_factor == 1 && components[2].v_samp_factor == 1); } void InitJPEGDataForYUV444(int w, int h, JPEGData* jpg) { jpg->width = w; jpg->height = h; jpg->max_h_samp_factor = 1; jpg->max_v_samp_factor = 1; jpg->MCU_rows = (h + 7) >> 3; jpg->MCU_cols = (w + 7) >> 3; jpg->quant.resize(3); jpg->components.resize(3); for (int i = 0; i < 3; ++i) { JPEGComponent* c = &jpg->components[i]; c->id = i; c->h_samp_factor = 1; c->v_samp_factor = 1; c->quant_idx = i; c->width_in_blocks = jpg->MCU_cols; c->height_in_blocks = jpg->MCU_rows; c->num_blocks = c->width_in_blocks * c->height_in_blocks; c->coeffs.resize(c->num_blocks * kDCTBlockSize); } } void SaveQuantTables(const int q[3][kDCTBlockSize], JPEGData* jpg) { const size_t kTableSize = kDCTBlockSize * sizeof(q[0][0]); jpg->quant.clear(); int num_tables = 0; for (size_t i = 0; i < jpg->components.size(); ++i) { JPEGComponent* comp = &jpg->components[i]; // Check if we have this quant table already. bool found = false; for (int j = 0; j < num_tables; ++j) { if (memcmp(&q[i][0], &jpg->quant[j].values[0], kTableSize) == 0) { comp->quant_idx = j; found = true; break; } } if (!found) { JPEGQuantTable table; memcpy(&table.values[0], &q[i][0], kTableSize); table.precision = 0; for (int k = 0; k < kDCTBlockSize; ++k) { assert(table.values[k] >= 0); assert(table.values[k] < (1 << 16)); if (table.values[k] > 0xff) { table.precision = 1; } } table.index = num_tables; comp->quant_idx = num_tables; jpg->quant.push_back(table); ++num_tables; } } } } // namespace guetzli ================================================ FILE: guetzli/jpeg_data.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Data structures that represent the contents of a jpeg file. #ifndef GUETZLI_JPEG_DATA_H_ #define GUETZLI_JPEG_DATA_H_ #include #include #include #include #include "guetzli/jpeg_error.h" namespace guetzli { static const int kDCTBlockSize = 64; static const int kMaxComponents = 4; static const int kMaxQuantTables = 4; static const int kMaxHuffmanTables = 4; static const int kJpegHuffmanMaxBitLength = 16; static const int kJpegHuffmanAlphabetSize = 256; static const int kJpegDCAlphabetSize = 12; static const int kMaxDHTMarkers = 512; static const uint8_t kDefaultQuantMatrix[2][64] = { { 16, 11, 10, 16, 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69, 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56, 68, 109, 103, 77, 24, 35, 55, 64, 81, 104, 113, 92, 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99 }, { 17, 18, 24, 47, 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56, 99, 99, 99, 99, 99, 47, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99 } }; const int kJPEGNaturalOrder[80] = { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, // extra entries for safety in decoder 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63 }; const int kJPEGZigZagOrder[64] = { 0, 1, 5, 6, 14, 15, 27, 28, 2, 4, 7, 13, 16, 26, 29, 42, 3, 8, 12, 17, 25, 30, 41, 43, 9, 11, 18, 24, 31, 40, 44, 53, 10, 19, 23, 32, 39, 45, 52, 54, 20, 22, 33, 38, 46, 51, 55, 60, 21, 34, 37, 47, 50, 56, 59, 61, 35, 36, 48, 49, 57, 58, 62, 63 }; // Quantization values for an 8x8 pixel block. struct JPEGQuantTable { JPEGQuantTable() : values(kDCTBlockSize), precision(0), index(0), is_last(true) {} std::vector values; int precision; // The index of this quantization table as it was parsed from the input JPEG. // Each DQT marker segment contains an 'index' field, and we save this index // here. Valid values are 0 to 3. int index; // Set to true if this table is the last one within its marker segment. bool is_last; }; // Huffman code and decoding lookup table used for DC and AC coefficients. struct JPEGHuffmanCode { JPEGHuffmanCode() : counts(kJpegHuffmanMaxBitLength + 1), values(kJpegHuffmanAlphabetSize + 1), slot_id(0), is_last(true) {} // Bit length histogram. std::vector counts; // Symbol values sorted by increasing bit lengths. std::vector values; // The index of the Huffman code in the current set of Huffman codes. For AC // component Huffman codes, 0x10 is added to the index. int slot_id; // Set to true if this Huffman code is the last one within its marker segment. bool is_last; }; // Huffman table indexes used for one component of one scan. struct JPEGComponentScanInfo { int comp_idx; int dc_tbl_idx; int ac_tbl_idx; }; // Contains information that is used in one scan. struct JPEGScanInfo { // Parameters used for progressive scans (named the same way as in the spec): // Ss : Start of spectral band in zig-zag sequence. // Se : End of spectral band in zig-zag sequence. // Ah : Successive approximation bit position, high. // Al : Successive approximation bit position, low. int Ss; int Se; int Ah; int Al; std::vector components; }; typedef int16_t coeff_t; // Represents one component of a jpeg file. struct JPEGComponent { JPEGComponent() : id(0), h_samp_factor(1), v_samp_factor(1), quant_idx(0), width_in_blocks(0), height_in_blocks(0) {} // One-byte id of the component. int id; // Horizontal and vertical sampling factors. // In interleaved mode, each minimal coded unit (MCU) has // h_samp_factor x v_samp_factor DCT blocks from this component. int h_samp_factor; int v_samp_factor; // The index of the quantization table used for this component. size_t quant_idx; // The dimensions of the component measured in 8x8 blocks. int width_in_blocks; int height_in_blocks; int num_blocks; // The DCT coefficients of this component, laid out block-by-block, divided // through the quantization matrix values. std::vector coeffs; }; // Represents a parsed jpeg file. struct JPEGData { JPEGData() : width(0), height(0), version(0), max_h_samp_factor(1), max_v_samp_factor(1), MCU_rows(0), MCU_cols(0), restart_interval(0), original_jpg(NULL), original_jpg_size(0), error(JPEG_OK) {} bool Is420() const; bool Is444() const; int width; int height; int version; int max_h_samp_factor; int max_v_samp_factor; int MCU_rows; int MCU_cols; int restart_interval; std::vector app_data; std::vector com_data; std::vector quant; std::vector huffman_code; std::vector components; std::vector scan_info; std::vector marker_order; std::vector inter_marker_data; std::string tail_data; const uint8_t* original_jpg; size_t original_jpg_size; JPEGReadError error; }; void InitJPEGDataForYUV444(int w, int h, JPEGData* jpg); void SaveQuantTables(const int q[3][kDCTBlockSize], JPEGData* jpg); } // namespace guetzli #endif // GUETZLI_JPEG_DATA_H_ ================================================ FILE: guetzli/jpeg_data_decoder.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/jpeg_data_decoder.h" #include "guetzli/output_image.h" namespace guetzli { // Mimic libjpeg's heuristics to guess jpeg color space. // Requires that the jpg has 3 components. bool HasYCbCrColorSpace(const JPEGData& jpg) { bool has_Adobe_marker = false; uint8_t Adobe_transform = 0; for (const std::string& app : jpg.app_data) { if (static_cast(app[0]) == 0xe0) { return true; } else if (static_cast(app[0]) == 0xee && app.size() >= 15) { has_Adobe_marker = true; Adobe_transform = app[14]; } } if (has_Adobe_marker) { return (Adobe_transform != 0); } const int cid0 = jpg.components[0].id; const int cid1 = jpg.components[1].id; const int cid2 = jpg.components[2].id; return (cid0 != 'R' || cid1 != 'G' || cid2 != 'B'); } std::vector DecodeJpegToRGB(const JPEGData& jpg) { if (jpg.components.size() == 1 || (jpg.components.size() == 3 && HasYCbCrColorSpace(jpg) && (jpg.Is420() || jpg.Is444()))) { OutputImage img(jpg.width, jpg.height); img.CopyFromJpegData(jpg); return img.ToSRGB(); } return std::vector(); } } // namespace guetzli ================================================ FILE: guetzli/jpeg_data_decoder.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Library to decode jpeg coefficients into an RGB image. #ifndef GUETZLI_JPEG_DATA_DECODER_H_ #define GUETZLI_JPEG_DATA_DECODER_H_ #include #include "guetzli/jpeg_data.h" namespace guetzli { // Decodes the parsed jpeg coefficients into an RGB image. // There can be only either 1 or 3 image components, in either case, an RGB // output image will be generated. // Only YUV420 and YUV444 sampling factors are supported. // Vector will be empty if a decoding error occurred. std::vector DecodeJpegToRGB(const JPEGData& jpg); // Mimic libjpeg's heuristics to guess jpeg color space. // Requires that the jpg has 3 components. bool HasYCbCrColorSpace(const JPEGData& jpg); } // namespace guetzli #endif // GUETZLI_JPEG_DATA_DECODER_H_ ================================================ FILE: guetzli/jpeg_data_encoder.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/jpeg_data_encoder.h" #include #include #include "guetzli/fdct.h" namespace guetzli { namespace { static const int kIQuantBits = 16; // Output of the DCT is upscaled by 16. static const int kDCTBits = kIQuantBits + 4; static const int kBias = 0x80 << (kDCTBits - 8); void Quantize(coeff_t* v, int iquant) { *v = (*v * iquant + kBias) >> kDCTBits; } // Single pixel rgb to 16-bit yuv conversion. // The returned yuv values are signed integers in the // range [-128, 127] inclusive. inline static void RGBToYUV16(const uint8_t* const rgb, coeff_t *out) { enum { FRAC = 16, HALF = 1 << (FRAC - 1) }; const int r = rgb[0]; const int g = rgb[1]; const int b = rgb[2]; out[0] = (19595 * r + 38469 * g + 7471 * b - (128 << 16) + HALF) >> FRAC; out[64] = (-11059 * r - 21709 * g + 32768 * b + HALF - 1) >> FRAC; out[128] = (32768 * r - 27439 * g - 5329 * b + HALF - 1) >> FRAC; } } // namespace void AddApp0Data(JPEGData* jpg) { const unsigned char kApp0Data[] = { 0xe0, 0x00, 0x10, // APP0 0x4a, 0x46, 0x49, 0x46, 0x00, // 'JFIF' 0x01, 0x01, // v1.01 0x00, 0x00, 0x01, 0x00, 0x01, // aspect ratio = 1:1 0x00, 0x00 // thumbnail width/height }; jpg->app_data.push_back( std::string(reinterpret_cast(kApp0Data), sizeof(kApp0Data))); } bool EncodeRGBToJpeg(const std::vector& rgb, int w, int h, const int* quant, JPEGData* jpg) { if (w < 0 || w >= 1 << 16 || h < 0 || h >= 1 << 16 || rgb.size() != 3 * w * h) { return false; } InitJPEGDataForYUV444(w, h, jpg); AddApp0Data(jpg); int iquant[3 * kDCTBlockSize]; int idx = 0; for (int i = 0; i < 3; ++i) { for (int j = 0; j < kDCTBlockSize; ++j) { int v = quant[idx]; jpg->quant[i].values[j] = v; iquant[idx++] = ((1 << kIQuantBits) + 1) / v; } } // Compute YUV444 DCT coefficients. int block_ix = 0; for (int block_y = 0; block_y < jpg->MCU_rows; ++block_y) { for (int block_x = 0; block_x < jpg->MCU_cols; ++block_x) { coeff_t block[3 * kDCTBlockSize]; // RGB->YUV transform. for (int iy = 0; iy < 8; ++iy) { for (int ix = 0; ix < 8; ++ix) { int y = std::min(h - 1, 8 * block_y + iy); int x = std::min(w - 1, 8 * block_x + ix); int p = y * w + x; RGBToYUV16(&rgb[3 * p], &block[8 * iy + ix]); } } // DCT for (int i = 0; i < 3; ++i) { ComputeBlockDCT(&block[i * kDCTBlockSize]); } // Quantization for (int i = 0; i < 3 * 64; ++i) { Quantize(&block[i], iquant[i]); } // Copy the resulting coefficients to *jpg. for (int i = 0; i < 3; ++i) { memcpy(&jpg->components[i].coeffs[block_ix * kDCTBlockSize], &block[i * kDCTBlockSize], kDCTBlockSize * sizeof(block[0])); } ++block_ix; } } return true; } bool EncodeRGBToJpeg(const std::vector& rgb, int w, int h, JPEGData* jpg) { static const int quant[3 * kDCTBlockSize] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, }; return EncodeRGBToJpeg(rgb, w, h, quant, jpg); } } // namespace guetzli ================================================ FILE: guetzli/jpeg_data_encoder.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_JPEG_DATA_ENCODER_H_ #define GUETZLI_JPEG_DATA_ENCODER_H_ #include #include "guetzli/jpeg_data.h" namespace guetzli { // Adds APP0 header data. void AddApp0Data(JPEGData* jpg); // Creates a JPEG from the rgb pixel data. Returns true on success. bool EncodeRGBToJpeg(const std::vector& rgb, int w, int h, JPEGData* jpg); // Creates a JPEG from the rgb pixel data. Returns true on success. The given // quantization table must have 3 * kDCTBlockSize values. bool EncodeRGBToJpeg(const std::vector& rgb, int w, int h, const int* quant, JPEGData* jpg); } // namespace guetzli #endif // GUETZLI_JPEG_DATA_ENCODER_H_ ================================================ FILE: guetzli/jpeg_data_reader.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/jpeg_data_reader.h" #include #include #include #include "guetzli/jpeg_huffman_decode.h" namespace guetzli { namespace { // Macros for commonly used error conditions. #define VERIFY_LEN(n) \ if (*pos + (n) > len) { \ fprintf(stderr, "Unexpected end of input: pos=%d need=%d len=%d\n", \ static_cast(*pos), static_cast(n), \ static_cast(len)); \ jpg->error = JPEG_UNEXPECTED_EOF; \ return false; \ } #define VERIFY_INPUT(var, low, high, code) \ if (var < low || var > high) { \ fprintf(stderr, "Invalid %s: %d\n", #var, static_cast(var)); \ jpg->error = JPEG_INVALID_ ## code; \ return false; \ } #define VERIFY_MARKER_END() \ if (start_pos + marker_len != *pos) { \ fprintf(stderr, "Invalid marker length: declared=%d actual=%d\n", \ static_cast(marker_len), \ static_cast(*pos - start_pos)); \ jpg->error = JPEG_WRONG_MARKER_SIZE; \ return false; \ } #define EXPECT_MARKER() \ if (pos + 2 > len || data[pos] != 0xff) { \ fprintf(stderr, "Marker byte (0xff) expected, found: %d " \ "pos=%d len=%d\n", \ (pos < len ? data[pos] : 0), static_cast(pos), \ static_cast(len)); \ jpg->error = JPEG_MARKER_BYTE_NOT_FOUND; \ return false; \ } inline int SignedLeftshift(int v, int s) { return (v >= 0) ? (v << s) : -((-v) << s); } // Returns ceil(a/b). inline int DivCeil(int a, int b) { return (a + b - 1) / b; } inline int ReadUint8(const uint8_t* data, size_t* pos) { return data[(*pos)++]; } inline int ReadUint16(const uint8_t* data, size_t* pos) { int v = (data[*pos] << 8) + data[*pos + 1]; *pos += 2; return v; } // Reads the Start of Frame (SOF) marker segment and fills in *jpg with the // parsed data. bool ProcessSOF(const uint8_t* data, const size_t len, JpegReadMode mode, size_t* pos, JPEGData* jpg) { if (jpg->width != 0) { fprintf(stderr, "Duplicate SOF marker.\n"); jpg->error = JPEG_DUPLICATE_SOF; return false; } const size_t start_pos = *pos; VERIFY_LEN(8); size_t marker_len = ReadUint16(data, pos); int precision = ReadUint8(data, pos); int height = ReadUint16(data, pos); int width = ReadUint16(data, pos); int num_components = ReadUint8(data, pos); VERIFY_INPUT(precision, 8, 8, PRECISION); VERIFY_INPUT(height, 1, 65535, HEIGHT); VERIFY_INPUT(width, 1, 65535, WIDTH); VERIFY_INPUT(num_components, 1, kMaxComponents, NUMCOMP); VERIFY_LEN(3 * num_components); jpg->height = height; jpg->width = width; jpg->components.resize(num_components); // Read sampling factors and quant table index for each component. std::vector ids_seen(256, false); for (size_t i = 0; i < jpg->components.size(); ++i) { const int id = ReadUint8(data, pos); if (ids_seen[id]) { // (cf. section B.2.2, syntax of Ci) fprintf(stderr, "Duplicate ID %d in SOF.\n", id); jpg->error = JPEG_DUPLICATE_COMPONENT_ID; return false; } ids_seen[id] = true; jpg->components[i].id = id; int factor = ReadUint8(data, pos); int h_samp_factor = factor >> 4; int v_samp_factor = factor & 0xf; VERIFY_INPUT(h_samp_factor, 1, 15, SAMP_FACTOR); VERIFY_INPUT(v_samp_factor, 1, 15, SAMP_FACTOR); jpg->components[i].h_samp_factor = h_samp_factor; jpg->components[i].v_samp_factor = v_samp_factor; jpg->components[i].quant_idx = ReadUint8(data, pos); jpg->max_h_samp_factor = std::max(jpg->max_h_samp_factor, h_samp_factor); jpg->max_v_samp_factor = std::max(jpg->max_v_samp_factor, v_samp_factor); } // We have checked above that none of the sampling factors are 0, so the max // sampling factors can not be 0. jpg->MCU_rows = DivCeil(jpg->height, jpg->max_v_samp_factor * 8); jpg->MCU_cols = DivCeil(jpg->width, jpg->max_h_samp_factor * 8); // Compute the block dimensions for each component. if (mode == JPEG_READ_ALL) { for (size_t i = 0; i < jpg->components.size(); ++i) { JPEGComponent* c = &jpg->components[i]; if (jpg->max_h_samp_factor % c->h_samp_factor != 0 || jpg->max_v_samp_factor % c->v_samp_factor != 0) { fprintf(stderr, "Non-integral subsampling ratios.\n"); jpg->error = JPEG_INVALID_SAMPLING_FACTORS; return false; } c->width_in_blocks = jpg->MCU_cols * c->h_samp_factor; c->height_in_blocks = jpg->MCU_rows * c->v_samp_factor; const uint64_t num_blocks = static_cast(c->width_in_blocks) * c->height_in_blocks; if (num_blocks > (1ull << 21)) { // Refuse to allocate more than 1 GB of memory for the coefficients, // that is 2M blocks x 64 coeffs x 2 bytes per coeff x max 4 components. // TODO(user) Add this limit to a GuetzliParams struct. fprintf(stderr, "Image too large.\n"); jpg->error = JPEG_IMAGE_TOO_LARGE; return false; } c->num_blocks = static_cast(num_blocks); c->coeffs.resize(c->num_blocks * kDCTBlockSize); } } VERIFY_MARKER_END(); return true; } // Reads the Start of Scan (SOS) marker segment and fills in *scan_info with the // parsed data. bool ProcessSOS(const uint8_t* data, const size_t len, size_t* pos, JPEGData* jpg) { const size_t start_pos = *pos; VERIFY_LEN(3); size_t marker_len = ReadUint16(data, pos); int comps_in_scan = ReadUint8(data, pos); VERIFY_INPUT(comps_in_scan, 1, static_cast(jpg->components.size()), COMPS_IN_SCAN); JPEGScanInfo scan_info; scan_info.components.resize(comps_in_scan); VERIFY_LEN(2 * comps_in_scan); std::vector ids_seen(256, false); for (int i = 0; i < comps_in_scan; ++i) { int id = ReadUint8(data, pos); if (ids_seen[id]) { // (cf. section B.2.3, regarding CSj) fprintf(stderr, "Duplicate ID %d in SOS.\n", id); jpg->error = JPEG_DUPLICATE_COMPONENT_ID; return false; } ids_seen[id] = true; bool found_index = false; for (size_t j = 0; j < jpg->components.size(); ++j) { if (jpg->components[j].id == id) { scan_info.components[i].comp_idx = j; found_index = true; } } if (!found_index) { fprintf(stderr, "SOS marker: Could not find component with id %d\n", id); jpg->error = JPEG_COMPONENT_NOT_FOUND; return false; } int c = ReadUint8(data, pos); int dc_tbl_idx = c >> 4; int ac_tbl_idx = c & 0xf; VERIFY_INPUT(dc_tbl_idx, 0, 3, HUFFMAN_INDEX); VERIFY_INPUT(ac_tbl_idx, 0, 3, HUFFMAN_INDEX); scan_info.components[i].dc_tbl_idx = dc_tbl_idx; scan_info.components[i].ac_tbl_idx = ac_tbl_idx; } VERIFY_LEN(3); scan_info.Ss = ReadUint8(data, pos); scan_info.Se = ReadUint8(data, pos); VERIFY_INPUT(scan_info.Ss, 0, 63, START_OF_SCAN); VERIFY_INPUT(scan_info.Se, scan_info.Ss, 63, END_OF_SCAN); int c = ReadUint8(data, pos); scan_info.Ah = c >> 4; scan_info.Al = c & 0xf; // Check that all the Huffman tables needed for this scan are defined. for (int i = 0; i < comps_in_scan; ++i) { bool found_dc_table = false; bool found_ac_table = false; for (size_t j = 0; j < jpg->huffman_code.size(); ++j) { int slot_id = jpg->huffman_code[j].slot_id; if (slot_id == scan_info.components[i].dc_tbl_idx) { found_dc_table = true; } else if (slot_id == scan_info.components[i].ac_tbl_idx + 16) { found_ac_table = true; } } if (scan_info.Ss == 0 && !found_dc_table) { fprintf(stderr, "SOS marker: Could not find DC Huffman table with index " "%d\n", scan_info.components[i].dc_tbl_idx); jpg->error = JPEG_HUFFMAN_TABLE_NOT_FOUND; return false; } if (scan_info.Se > 0 && !found_ac_table) { fprintf(stderr, "SOS marker: Could not find AC Huffman table with index " "%d\n", scan_info.components[i].ac_tbl_idx); jpg->error = JPEG_HUFFMAN_TABLE_NOT_FOUND; return false; } } jpg->scan_info.push_back(scan_info); VERIFY_MARKER_END(); return true; } // Reads the Define Huffman Table (DHT) marker segment and fills in *jpg with // the parsed data. Builds the Huffman decoding table in either dc_huff_lut or // ac_huff_lut, depending on the type and solt_id of Huffman code being read. bool ProcessDHT(const uint8_t* data, const size_t len, JpegReadMode mode, std::vector* dc_huff_lut, std::vector* ac_huff_lut, size_t* pos, JPEGData* jpg) { const size_t start_pos = *pos; VERIFY_LEN(2); size_t marker_len = ReadUint16(data, pos); if (marker_len == 2) { fprintf(stderr, "DHT marker: no Huffman table found\n"); jpg->error = JPEG_EMPTY_DHT; return false; } while (*pos < start_pos + marker_len) { VERIFY_LEN(1 + kJpegHuffmanMaxBitLength); JPEGHuffmanCode huff; huff.slot_id = ReadUint8(data, pos); int huffman_index = huff.slot_id; int is_ac_table = (huff.slot_id & 0x10) != 0; HuffmanTableEntry* huff_lut; if (is_ac_table) { huffman_index -= 0x10; VERIFY_INPUT(huffman_index, 0, 3, HUFFMAN_INDEX); huff_lut = &(*ac_huff_lut)[huffman_index * kJpegHuffmanLutSize]; } else { VERIFY_INPUT(huffman_index, 0, 3, HUFFMAN_INDEX); huff_lut = &(*dc_huff_lut)[huffman_index * kJpegHuffmanLutSize]; } huff.counts[0] = 0; int total_count = 0; int space = 1 << kJpegHuffmanMaxBitLength; int max_depth = 1; for (int i = 1; i <= kJpegHuffmanMaxBitLength; ++i) { int count = ReadUint8(data, pos); if (count != 0) { max_depth = i; } huff.counts[i] = count; total_count += count; space -= count * (1 << (kJpegHuffmanMaxBitLength - i)); } if (is_ac_table) { VERIFY_INPUT(total_count, 0, kJpegHuffmanAlphabetSize, HUFFMAN_CODE); } else { VERIFY_INPUT(total_count, 0, kJpegDCAlphabetSize, HUFFMAN_CODE); } VERIFY_LEN(total_count); std::vector values_seen(256, false); for (int i = 0; i < total_count; ++i) { uint8_t value = ReadUint8(data, pos); if (!is_ac_table) { VERIFY_INPUT(value, 0, kJpegDCAlphabetSize - 1, HUFFMAN_CODE); } if (values_seen[value]) { fprintf(stderr, "Duplicate Huffman code value %d\n", value); jpg->error = JPEG_INVALID_HUFFMAN_CODE; return false; } values_seen[value] = true; huff.values[i] = value; } // Add an invalid symbol that will have the all 1 code. ++huff.counts[max_depth]; huff.values[total_count] = kJpegHuffmanAlphabetSize; space -= (1 << (kJpegHuffmanMaxBitLength - max_depth)); if (space < 0) { fprintf(stderr, "Invalid Huffman code lengths.\n"); jpg->error = JPEG_INVALID_HUFFMAN_CODE; return false; } else if (space > 0 && huff_lut[0].value != 0xffff) { // Re-initialize the values to an invalid symbol so that we can recognize // it when reading the bit stream using a Huffman code with space > 0. for (int i = 0; i < kJpegHuffmanLutSize; ++i) { huff_lut[i].bits = 0; huff_lut[i].value = 0xffff; } } huff.is_last = (*pos == start_pos + marker_len); if (mode == JPEG_READ_ALL && !BuildJpegHuffmanTable(&huff.counts[0], &huff.values[0], huff_lut)) { fprintf(stderr, "Failed to build Huffman table.\n"); jpg->error = JPEG_INVALID_HUFFMAN_CODE; return false; } jpg->huffman_code.push_back(huff); } VERIFY_MARKER_END(); return true; } // Reads the Define Quantization Table (DQT) marker segment and fills in *jpg // with the parsed data. bool ProcessDQT(const uint8_t* data, const size_t len, size_t* pos, JPEGData* jpg) { const size_t start_pos = *pos; VERIFY_LEN(2); size_t marker_len = ReadUint16(data, pos); if (marker_len == 2) { fprintf(stderr, "DQT marker: no quantization table found\n"); jpg->error = JPEG_EMPTY_DQT; return false; } while (*pos < start_pos + marker_len && jpg->quant.size() < kMaxQuantTables) { VERIFY_LEN(1); int quant_table_index = ReadUint8(data, pos); int quant_table_precision = quant_table_index >> 4; quant_table_index &= 0xf; VERIFY_INPUT(quant_table_index, 0, 3, QUANT_TBL_INDEX); VERIFY_LEN((quant_table_precision ? 2 : 1) * kDCTBlockSize); JPEGQuantTable table; table.index = quant_table_index; table.precision = quant_table_precision; for (int i = 0; i < kDCTBlockSize; ++i) { int quant_val = quant_table_precision ? ReadUint16(data, pos) : ReadUint8(data, pos); VERIFY_INPUT(quant_val, 1, 65535, QUANT_VAL); table.values[kJPEGNaturalOrder[i]] = quant_val; } table.is_last = (*pos == start_pos + marker_len); jpg->quant.push_back(table); } VERIFY_MARKER_END(); return true; } // Reads the DRI marker and saved the restart interval into *jpg. bool ProcessDRI(const uint8_t* data, const size_t len, size_t* pos, JPEGData* jpg) { if (jpg->restart_interval > 0) { fprintf(stderr, "Duplicate DRI marker.\n"); jpg->error = JPEG_DUPLICATE_DRI; return false; } const size_t start_pos = *pos; VERIFY_LEN(4); size_t marker_len = ReadUint16(data, pos); int restart_interval = ReadUint16(data, pos); jpg->restart_interval = restart_interval; VERIFY_MARKER_END(); return true; } // Saves the APP marker segment as a string to *jpg. bool ProcessAPP(const uint8_t* data, const size_t len, size_t* pos, JPEGData* jpg) { VERIFY_LEN(2); size_t marker_len = ReadUint16(data, pos); VERIFY_INPUT(marker_len, 2, 65535, MARKER_LEN); VERIFY_LEN(marker_len - 2); // Save the marker type together with the app data. std::string app_str(reinterpret_cast( &data[*pos - 3]), marker_len + 1); *pos += marker_len - 2; jpg->app_data.push_back(app_str); return true; } // Saves the COM marker segment as a string to *jpg. bool ProcessCOM(const uint8_t* data, const size_t len, size_t* pos, JPEGData* jpg) { VERIFY_LEN(2); size_t marker_len = ReadUint16(data, pos); VERIFY_INPUT(marker_len, 2, 65535, MARKER_LEN); VERIFY_LEN(marker_len - 2); std::string com_str(reinterpret_cast( &data[*pos - 2]), marker_len); *pos += marker_len - 2; jpg->com_data.push_back(com_str); return true; } // Helper structure to read bits from the entropy coded data segment. struct BitReaderState { BitReaderState(const uint8_t* data, const size_t len, size_t pos) : data_(data), len_(len) { Reset(pos); } void Reset(size_t pos) { pos_ = pos; val_ = 0; bits_left_ = 0; next_marker_pos_ = len_ - 2; FillBitWindow(); } // Returns the next byte and skips the 0xff/0x00 escape sequences. uint8_t GetNextByte() { if (pos_ >= next_marker_pos_) { ++pos_; return 0; } uint8_t c = data_[pos_++]; if (c == 0xff) { uint8_t escape = data_[pos_]; if (escape == 0) { ++pos_; } else { // 0xff was followed by a non-zero byte, which means that we found the // start of the next marker segment. next_marker_pos_ = pos_ - 1; } } return c; } void FillBitWindow() { if (bits_left_ <= 16) { while (bits_left_ <= 56) { val_ <<= 8; val_ |= (uint64_t)GetNextByte(); bits_left_ += 8; } } } int ReadBits(int nbits) { FillBitWindow(); uint64_t val = (val_ >> (bits_left_ - nbits)) & ((1ULL << nbits) - 1); bits_left_ -= nbits; return val; } // Sets *pos to the next stream position where parsing should continue. // Returns false if the stream ended too early. bool FinishStream(size_t* pos) { // Give back some bytes that we did not use. int unused_bytes_left = bits_left_ >> 3; while (unused_bytes_left-- > 0) { --pos_; // If we give back a 0 byte, we need to check if it was a 0xff/0x00 escape // sequence, and if yes, we need to give back one more byte. if (pos_ < next_marker_pos_ && data_[pos_] == 0 && data_[pos_ - 1] == 0xff) { --pos_; } } if (pos_ > next_marker_pos_) { // Data ran out before the scan was complete. fprintf(stderr, "Unexpected end of scan.\n"); return false; } *pos = pos_; return true; } const uint8_t* data_; const size_t len_; size_t pos_; uint64_t val_; int bits_left_; size_t next_marker_pos_; }; // Returns the next Huffman-coded symbol. int ReadSymbol(const HuffmanTableEntry* table, BitReaderState* br) { int nbits; br->FillBitWindow(); int val = (br->val_ >> (br->bits_left_ - 8)) & 0xff; table += val; nbits = table->bits - 8; if (nbits > 0) { br->bits_left_ -= 8; table += table->value; val = (br->val_ >> (br->bits_left_ - nbits)) & ((1 << nbits) - 1); table += val; } br->bits_left_ -= table->bits; return table->value; } // Returns the DC diff or AC value for extra bits value x and prefix code s. // See Tables F.1 and F.2 of the spec. int HuffExtend(int x, int s) { return (x < (1 << (s - 1)) ? x - (1 << s) + 1 : x); } // Decodes one 8x8 block of DCT coefficients from the bit stream. bool DecodeDCTBlock(const HuffmanTableEntry* dc_huff, const HuffmanTableEntry* ac_huff, int Ss, int Se, int Al, int* eobrun, BitReaderState* br, JPEGData* jpg, coeff_t* last_dc_coeff, coeff_t* coeffs) { int s; int r; bool eobrun_allowed = Ss > 0; if (Ss == 0) { s = ReadSymbol(dc_huff, br); if (s >= kJpegDCAlphabetSize) { fprintf(stderr, "Invalid Huffman symbol %d for DC coefficient.\n", s); jpg->error = JPEG_INVALID_SYMBOL; return false; } if (s > 0) { r = br->ReadBits(s); s = HuffExtend(r, s); } s += *last_dc_coeff; const int dc_coeff = SignedLeftshift(s, Al); coeffs[0] = dc_coeff; if (dc_coeff != coeffs[0]) { fprintf(stderr, "Invalid DC coefficient %d\n", dc_coeff); jpg->error = JPEG_NON_REPRESENTABLE_DC_COEFF; return false; } *last_dc_coeff = s; ++Ss; } if (Ss > Se) { return true; } if (*eobrun > 0) { --(*eobrun); return true; } for (int k = Ss; k <= Se; k++) { s = ReadSymbol(ac_huff, br); if (s >= kJpegHuffmanAlphabetSize) { fprintf(stderr, "Invalid Huffman symbol %d for AC coefficient %d\n", s, k); jpg->error = JPEG_INVALID_SYMBOL; return false; } r = s >> 4; s &= 15; if (s > 0) { k += r; if (k > Se) { fprintf(stderr, "Out-of-band coefficient %d band was %d-%d\n", k, Ss, Se); jpg->error = JPEG_OUT_OF_BAND_COEFF; return false; } if (s + Al >= kJpegDCAlphabetSize) { fprintf(stderr, "Out of range AC coefficient value: s=%d Al=%d k=%d\n", s, Al, k); jpg->error = JPEG_NON_REPRESENTABLE_AC_COEFF; return false; } r = br->ReadBits(s); s = HuffExtend(r, s); coeffs[kJPEGNaturalOrder[k]] = SignedLeftshift(s, Al); } else if (r == 15) { k += 15; } else { *eobrun = 1 << r; if (r > 0) { if (!eobrun_allowed) { fprintf(stderr, "End-of-block run crossing DC coeff.\n"); jpg->error = JPEG_EOB_RUN_TOO_LONG; return false; } *eobrun += br->ReadBits(r); } break; } } --(*eobrun); return true; } bool RefineDCTBlock(const HuffmanTableEntry* ac_huff, int Ss, int Se, int Al, int* eobrun, BitReaderState* br, JPEGData* jpg, coeff_t* coeffs) { bool eobrun_allowed = Ss > 0; if (Ss == 0) { int s = br->ReadBits(1); coeff_t dc_coeff = coeffs[0]; dc_coeff |= s << Al; coeffs[0] = dc_coeff; ++Ss; } if (Ss > Se) { return true; } int p1 = 1 << Al; int m1 = -(1 << Al); int k = Ss; int r; int s; bool in_zero_run = false; if (*eobrun <= 0) { for (; k <= Se; k++) { s = ReadSymbol(ac_huff, br); if (s >= kJpegHuffmanAlphabetSize) { fprintf(stderr, "Invalid Huffman symbol %d for AC coefficient %d\n", s, k); jpg->error = JPEG_INVALID_SYMBOL; return false; } r = s >> 4; s &= 15; if (s) { if (s != 1) { fprintf(stderr, "Invalid Huffman symbol %d for AC coefficient %d\n", s, k); jpg->error = JPEG_INVALID_SYMBOL; return false; } s = br->ReadBits(1) ? p1 : m1; in_zero_run = false; } else { if (r != 15) { *eobrun = 1 << r; if (r > 0) { if (!eobrun_allowed) { fprintf(stderr, "End-of-block run crossing DC coeff.\n"); jpg->error = JPEG_EOB_RUN_TOO_LONG; return false; } *eobrun += br->ReadBits(r); } break; } in_zero_run = true; } do { coeff_t thiscoef = coeffs[kJPEGNaturalOrder[k]]; if (thiscoef != 0) { if (br->ReadBits(1)) { if ((thiscoef & p1) == 0) { if (thiscoef >= 0) { thiscoef += p1; } else { thiscoef += m1; } } } coeffs[kJPEGNaturalOrder[k]] = thiscoef; } else { if (--r < 0) { break; } } k++; } while (k <= Se); if (s) { if (k > Se) { fprintf(stderr, "Out-of-band coefficient %d band was %d-%d\n", k, Ss, Se); jpg->error = JPEG_OUT_OF_BAND_COEFF; return false; } coeffs[kJPEGNaturalOrder[k]] = s; } } } if (in_zero_run) { fprintf(stderr, "Extra zero run before end-of-block.\n"); jpg->error = JPEG_EXTRA_ZERO_RUN; return false; } if (*eobrun > 0) { for (; k <= Se; k++) { coeff_t thiscoef = coeffs[kJPEGNaturalOrder[k]]; if (thiscoef != 0) { if (br->ReadBits(1)) { if ((thiscoef & p1) == 0) { if (thiscoef >= 0) { thiscoef += p1; } else { thiscoef += m1; } } } coeffs[kJPEGNaturalOrder[k]] = thiscoef; } } } --(*eobrun); return true; } bool ProcessRestart(const uint8_t* data, const size_t len, int* next_restart_marker, BitReaderState* br, JPEGData* jpg) { size_t pos = 0; if (!br->FinishStream(&pos)) { jpg->error = JPEG_INVALID_SCAN; return false; } int expected_marker = 0xd0 + *next_restart_marker; EXPECT_MARKER(); int marker = data[pos + 1]; if (marker != expected_marker) { fprintf(stderr, "Did not find expected restart marker %d actual=%d\n", expected_marker, marker); jpg->error = JPEG_WRONG_RESTART_MARKER; return false; } br->Reset(pos + 2); *next_restart_marker += 1; *next_restart_marker &= 0x7; return true; } bool ProcessScan(const uint8_t* data, const size_t len, const std::vector& dc_huff_lut, const std::vector& ac_huff_lut, uint16_t scan_progression[kMaxComponents][kDCTBlockSize], bool is_progressive, size_t* pos, JPEGData* jpg) { if (!ProcessSOS(data, len, pos, jpg)) { return false; } JPEGScanInfo* scan_info = &jpg->scan_info.back(); bool is_interleaved = (scan_info->components.size() > 1); int MCUs_per_row; int MCU_rows; if (is_interleaved) { MCUs_per_row = jpg->MCU_cols; MCU_rows = jpg->MCU_rows; } else { const JPEGComponent& c = jpg->components[scan_info->components[0].comp_idx]; MCUs_per_row = DivCeil(jpg->width * c.h_samp_factor, 8 * jpg->max_h_samp_factor); MCU_rows = DivCeil(jpg->height * c.v_samp_factor, 8 * jpg->max_v_samp_factor); } coeff_t last_dc_coeff[kMaxComponents] = {0}; BitReaderState br(data, len, *pos); int restarts_to_go = jpg->restart_interval; int next_restart_marker = 0; int eobrun = -1; int block_scan_index = 0; const int Al = is_progressive ? scan_info->Al : 0; const int Ah = is_progressive ? scan_info->Ah : 0; const int Ss = is_progressive ? scan_info->Ss : 0; const int Se = is_progressive ? scan_info->Se : 63; const uint16_t scan_bitmask = Ah == 0 ? (0xffff << Al) : (1u << Al); const uint16_t refinement_bitmask = (1 << Al) - 1; for (size_t i = 0; i < scan_info->components.size(); ++i) { int comp_idx = scan_info->components[i].comp_idx; for (int k = Ss; k <= Se; ++k) { if (scan_progression[comp_idx][k] & scan_bitmask) { fprintf(stderr, "Overlapping scans: component=%d k=%d prev_mask=%d " "cur_mask=%d\n", comp_idx, k, scan_progression[i][k], scan_bitmask); jpg->error = JPEG_OVERLAPPING_SCANS; return false; } if (scan_progression[comp_idx][k] & refinement_bitmask) { fprintf(stderr, "Invalid scan order, a more refined scan was already " "done: component=%d k=%d prev_mask=%d cur_mask=%d\n", comp_idx, k, scan_progression[i][k], scan_bitmask); jpg->error = JPEG_INVALID_SCAN_ORDER; return false; } scan_progression[comp_idx][k] |= scan_bitmask; } } if (Al > 10) { fprintf(stderr, "Scan parameter Al=%d is not supported in guetzli.\n", Al); jpg->error = JPEG_NON_REPRESENTABLE_AC_COEFF; return false; } for (int mcu_y = 0; mcu_y < MCU_rows; ++mcu_y) { for (int mcu_x = 0; mcu_x < MCUs_per_row; ++mcu_x) { // Handle the restart intervals. if (jpg->restart_interval > 0) { if (restarts_to_go == 0) { if (ProcessRestart(data, len, &next_restart_marker, &br, jpg)) { restarts_to_go = jpg->restart_interval; memset(last_dc_coeff, 0, sizeof(last_dc_coeff)); if (eobrun > 0) { fprintf(stderr, "End-of-block run too long.\n"); jpg->error = JPEG_EOB_RUN_TOO_LONG; return false; } eobrun = -1; // fresh start } else { return false; } } --restarts_to_go; } // Decode one MCU. for (size_t i = 0; i < scan_info->components.size(); ++i) { JPEGComponentScanInfo* si = &scan_info->components[i]; JPEGComponent* c = &jpg->components[si->comp_idx]; const HuffmanTableEntry* dc_lut = &dc_huff_lut[si->dc_tbl_idx * kJpegHuffmanLutSize]; const HuffmanTableEntry* ac_lut = &ac_huff_lut[si->ac_tbl_idx * kJpegHuffmanLutSize]; int nblocks_y = is_interleaved ? c->v_samp_factor : 1; int nblocks_x = is_interleaved ? c->h_samp_factor : 1; for (int iy = 0; iy < nblocks_y; ++iy) { for (int ix = 0; ix < nblocks_x; ++ix) { int block_y = mcu_y * nblocks_y + iy; int block_x = mcu_x * nblocks_x + ix; int block_idx = block_y * c->width_in_blocks + block_x; coeff_t* coeffs = &c->coeffs[block_idx * kDCTBlockSize]; if (Ah == 0) { if (!DecodeDCTBlock(dc_lut, ac_lut, Ss, Se, Al, &eobrun, &br, jpg, &last_dc_coeff[si->comp_idx], coeffs)) { return false; } } else { if (!RefineDCTBlock(ac_lut, Ss, Se, Al, &eobrun, &br, jpg, coeffs)) { return false; } } ++block_scan_index; } } } } } if (eobrun > 0) { fprintf(stderr, "End-of-block run too long.\n"); jpg->error = JPEG_EOB_RUN_TOO_LONG; return false; } if (!br.FinishStream(pos)) { jpg->error = JPEG_INVALID_SCAN; return false; } if (*pos > len) { fprintf(stderr, "Unexpected end of file during scan. pos=%d len=%d\n", static_cast(*pos), static_cast(len)); jpg->error = JPEG_UNEXPECTED_EOF; return false; } return true; } // Changes the quant_idx field of the components to refer to the index of the // quant table in the jpg->quant array. bool FixupIndexes(JPEGData* jpg) { for (size_t i = 0; i < jpg->components.size(); ++i) { JPEGComponent* c = &jpg->components[i]; bool found_index = false; for (size_t j = 0; j < jpg->quant.size(); ++j) { if (jpg->quant[j].index == c->quant_idx) { c->quant_idx = j; found_index = true; break; } } if (!found_index) { fprintf(stderr, "Quantization table with index %zd not found\n", c->quant_idx); jpg->error = JPEG_QUANT_TABLE_NOT_FOUND; return false; } } return true; } size_t FindNextMarker(const uint8_t* data, const size_t len, size_t pos) { // kIsValidMarker[i] == 1 means (0xc0 + i) is a valid marker. static const uint8_t kIsValidMarker[] = { 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, }; size_t num_skipped = 0; while (pos + 1 < len && (data[pos] != 0xff || data[pos + 1] < 0xc0 || !kIsValidMarker[data[pos + 1] - 0xc0])) { ++pos; ++num_skipped; } return num_skipped; } } // namespace bool ReadJpeg(const uint8_t* data, const size_t len, JpegReadMode mode, JPEGData* jpg) { size_t pos = 0; // Check SOI marker. EXPECT_MARKER(); int marker = data[pos + 1]; pos += 2; if (marker != 0xd8) { fprintf(stderr, "Did not find expected SOI marker, actual=%d\n", marker); jpg->error = JPEG_SOI_NOT_FOUND; return false; } int lut_size = kMaxHuffmanTables * kJpegHuffmanLutSize; std::vector dc_huff_lut(lut_size); std::vector ac_huff_lut(lut_size); bool found_sof = false; uint16_t scan_progression[kMaxComponents][kDCTBlockSize] = { { 0 } }; bool is_progressive = false; // default do { // Read next marker. size_t num_skipped = FindNextMarker(data, len, pos); if (num_skipped > 0) { // Add a fake marker to indicate arbitrary in-between-markers data. jpg->marker_order.push_back(0xff); jpg->inter_marker_data.push_back( std::string(reinterpret_cast(&data[pos]), num_skipped)); pos += num_skipped; } EXPECT_MARKER(); marker = data[pos + 1]; pos += 2; bool ok = true; switch (marker) { case 0xc0: case 0xc1: case 0xc2: is_progressive = (marker == 0xc2); ok = ProcessSOF(data, len, mode, &pos, jpg); found_sof = true; break; case 0xc4: ok = ProcessDHT(data, len, mode, &dc_huff_lut, &ac_huff_lut, &pos, jpg); break; case 0xd0: case 0xd1: case 0xd2: case 0xd3: case 0xd4: case 0xd5: case 0xd6: case 0xd7: // RST markers do not have any data. break; case 0xd9: // Found end marker. break; case 0xda: if (mode == JPEG_READ_ALL) { ok = ProcessScan(data, len, dc_huff_lut, ac_huff_lut, scan_progression, is_progressive, &pos, jpg); } break; case 0xdb: ok = ProcessDQT(data, len, &pos, jpg); break; case 0xdd: ok = ProcessDRI(data, len, &pos, jpg); break; case 0xe0: case 0xe1: case 0xe2: case 0xe3: case 0xe4: case 0xe5: case 0xe6: case 0xe7: case 0xe8: case 0xe9: case 0xea: case 0xeb: case 0xec: case 0xed: case 0xee: case 0xef: if (mode != JPEG_READ_TABLES) { ok = ProcessAPP(data, len, &pos, jpg); } break; case 0xfe: if (mode != JPEG_READ_TABLES) { ok = ProcessCOM(data, len, &pos, jpg); } break; default: fprintf(stderr, "Unsupported marker: %d pos=%d len=%d\n", marker, static_cast(pos), static_cast(len)); jpg->error = JPEG_UNSUPPORTED_MARKER; ok = false; break; } if (!ok) { return false; } jpg->marker_order.push_back(marker); if (mode == JPEG_READ_HEADER && found_sof) { break; } } while (marker != 0xd9); if (!found_sof) { fprintf(stderr, "Missing SOF marker.\n"); jpg->error = JPEG_SOF_NOT_FOUND; return false; } // Supplemental checks. if (mode == JPEG_READ_ALL) { if (pos < len) { jpg->tail_data.assign(reinterpret_cast(&data[pos]), len - pos); } if (!FixupIndexes(jpg)) { return false; } if (jpg->huffman_code.size() == 0) { // Section B.2.4.2: "If a table has never been defined for a particular // destination, then when this destination is specified in a scan header, // the results are unpredictable." fprintf(stderr, "Need at least one Huffman code table.\n"); jpg->error = JPEG_HUFFMAN_TABLE_ERROR; return false; } if (jpg->huffman_code.size() >= kMaxDHTMarkers) { fprintf(stderr, "Too many Huffman tables.\n"); jpg->error = JPEG_HUFFMAN_TABLE_ERROR; return false; } } return true; } bool ReadJpeg(const std::string& data, JpegReadMode mode, JPEGData* jpg) { return ReadJpeg(reinterpret_cast(data.data()), static_cast(data.size()), mode, jpg); } } // namespace guetzli ================================================ FILE: guetzli/jpeg_data_reader.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Functions for reading a jpeg byte stream into a JPEGData object. #ifndef GUETZLI_JPEG_DATA_READER_H_ #define GUETZLI_JPEG_DATA_READER_H_ #include #include #include #include "guetzli/jpeg_data.h" namespace guetzli { enum JpegReadMode { JPEG_READ_HEADER, // only basic headers JPEG_READ_TABLES, // headers and tables (quant, Huffman, ...) JPEG_READ_ALL, // everything }; // Parses the jpeg stream contained in data[*pos ... len) and fills in *jpg with // the parsed information. // If mode is JPEG_READ_HEADER, it fills in only the image dimensions in *jpg. // Returns false if the data is not valid jpeg, or if it contains an unsupported // jpeg feature. bool ReadJpeg(const uint8_t* data, const size_t len, JpegReadMode mode, JPEGData* jpg); // string variant bool ReadJpeg(const std::string& data, JpegReadMode mode, JPEGData* jpg); } // namespace guetzli #endif // GUETZLI_JPEG_DATA_READER_H_ ================================================ FILE: guetzli/jpeg_data_writer.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/jpeg_data_writer.h" #include #include #include #include "guetzli/entropy_encode.h" #include "guetzli/fast_log.h" #include "guetzli/jpeg_bit_writer.h" namespace guetzli { namespace { static const int kJpegPrecision = 8; // Writes len bytes from buf, using the out callback. inline bool JPEGWrite(JPEGOutput out, const uint8_t* buf, size_t len) { static const size_t kBlockSize = 1u << 30; size_t pos = 0; while (len - pos > kBlockSize) { if (!out.Write(buf + pos, kBlockSize)) { return false; } pos += kBlockSize; } return out.Write(buf + pos, len - pos); } // Writes a string using the out callback. inline bool JPEGWrite(JPEGOutput out, const std::string& s) { const uint8_t* data = reinterpret_cast(&s[0]); return JPEGWrite(out, data, s.size()); } bool EncodeMetadata(const JPEGData& jpg, bool strip_metadata, JPEGOutput out) { if (strip_metadata) { const uint8_t kApp0Data[] = { 0xff, 0xe0, 0x00, 0x10, // APP0 0x4a, 0x46, 0x49, 0x46, 0x00, // 'JFIF' 0x01, 0x01, // v1.01 0x00, 0x00, 0x01, 0x00, 0x01, // aspect ratio = 1:1 0x00, 0x00 // thumbnail width/height }; return JPEGWrite(out, kApp0Data, sizeof(kApp0Data)); } bool ok = true; for (size_t i = 0; i < jpg.app_data.size(); ++i) { uint8_t data[1] = { 0xff }; ok = ok && JPEGWrite(out, data, sizeof(data)); ok = ok && JPEGWrite(out, jpg.app_data[i]); } for (size_t i = 0; i < jpg.com_data.size(); ++i) { uint8_t data[2] = { 0xff, 0xfe }; ok = ok && JPEGWrite(out, data, sizeof(data)); ok = ok && JPEGWrite(out, jpg.com_data[i]); } return ok; } bool EncodeDQT(const std::vector& quant, JPEGOutput out) { int marker_len = 2; for (size_t i = 0; i < quant.size(); ++i) { marker_len += 1 + (quant[i].precision ? 2 : 1) * kDCTBlockSize; } std::vector data(marker_len + 2); size_t pos = 0; data[pos++] = 0xff; data[pos++] = 0xdb; data[pos++] = marker_len >> 8; data[pos++] = marker_len & 0xff; for (size_t i = 0; i < quant.size(); ++i) { const JPEGQuantTable& table = quant[i]; data[pos++] = (table.precision << 4) + table.index; for (int k = 0; k < kDCTBlockSize; ++k) { int val = table.values[kJPEGNaturalOrder[k]]; if (table.precision) { data[pos++] = val >> 8; } data[pos++] = val & 0xff; } } return JPEGWrite(out, &data[0], pos); } bool EncodeSOF(const JPEGData& jpg, JPEGOutput out) { const size_t ncomps = jpg.components.size(); const size_t marker_len = 8 + 3 * ncomps; std::vector data(marker_len + 2); size_t pos = 0; data[pos++] = 0xff; data[pos++] = 0xc1; data[pos++] = static_cast(marker_len >> 8); data[pos++] = marker_len & 0xff; data[pos++] = kJpegPrecision; data[pos++] = jpg.height >> 8; data[pos++] = jpg.height & 0xff; data[pos++] = jpg.width >> 8; data[pos++] = jpg.width & 0xff; data[pos++] = static_cast(ncomps); for (size_t i = 0; i < ncomps; ++i) { data[pos++] = jpg.components[i].id; data[pos++] = ((jpg.components[i].h_samp_factor << 4) | (jpg.components[i].v_samp_factor)); const size_t quant_idx = jpg.components[i].quant_idx; if (quant_idx >= jpg.quant.size()) { return false; } data[pos++] = jpg.quant[quant_idx].index; } return JPEGWrite(out, &data[0], pos); } // Builds a JPEG-style huffman code from the given bit depths. void BuildHuffmanCode(uint8_t* depth, int* counts, int* values) { for (int i = 0; i < JpegHistogram::kSize; ++i) { if (depth[i] > 0) { ++counts[depth[i]]; } } int offset[kJpegHuffmanMaxBitLength + 1] = { 0 }; for (int i = 1; i <= kJpegHuffmanMaxBitLength; ++i) { offset[i] = offset[i - 1] + counts[i - 1]; } for (int i = 0; i < JpegHistogram::kSize; ++i) { if (depth[i] > 0) { values[offset[depth[i]]++] = i; } } } void BuildHuffmanCodeTable(const int* counts, const int* values, HuffmanCodeTable* table) { int huffcode[256]; int huffsize[256]; int p = 0; for (int l = 1; l <= kJpegHuffmanMaxBitLength; ++l) { int i = counts[l]; while (i--) huffsize[p++] = l; } if (p == 0) return; huffsize[p - 1] = 0; int lastp = p - 1; int code = 0; int si = huffsize[0]; p = 0; while (huffsize[p]) { while ((huffsize[p]) == si) { huffcode[p++] = code; code++; } code <<= 1; si++; } for (p = 0; p < lastp; p++) { int i = values[p]; table->depth[i] = huffsize[p]; table->code[i] = huffcode[p]; } } } // namespace // Updates ac_histogram with the counts of the AC symbols that will be added by // a sequential jpeg encoder for this block. Every symbol is counted twice so // that we can add a fake symbol at the end with count 1 to be the last (least // frequent) symbol with the all 1 code. void UpdateACHistogramForDCTBlock(const coeff_t* coeffs, JpegHistogram* ac_histogram) { int r = 0; for (int k = 1; k < 64; ++k) { coeff_t coeff = coeffs[kJPEGNaturalOrder[k]]; if (coeff == 0) { r++; continue; } while (r > 15) { ac_histogram->Add(0xf0); r -= 16; } int nbits = Log2FloorNonZero(std::abs(coeff)) + 1; int symbol = (r << 4) + nbits; ac_histogram->Add(symbol); r = 0; } if (r > 0) { ac_histogram->Add(0); } } size_t HistogramHeaderCost(const JpegHistogram& histo) { size_t header_bits = 17 * 8; for (int i = 0; i + 1 < JpegHistogram::kSize; ++i) { if (histo.counts[i] > 0) { header_bits += 8; } } return header_bits; } size_t HistogramEntropyCost(const JpegHistogram& histo, const uint8_t depths[256]) { size_t bits = 0; for (int i = 0; i + 1 < JpegHistogram::kSize; ++i) { // JpegHistogram::Add() counts every symbol twice, so we have to divide by // two here. bits += (histo.counts[i] / 2) * (depths[i] + (i & 0xf)); } // Estimate escape byte rate to be 0.75/256. bits += (bits * 3 + 512) >> 10; return bits; } void BuildDCHistograms(const JPEGData& jpg, JpegHistogram* histo) { for (size_t i = 0; i < jpg.components.size(); ++i) { const JPEGComponent& c = jpg.components[i]; JpegHistogram* dc_histogram = &histo[i]; coeff_t last_dc_coeff = 0; for (int mcu_y = 0; mcu_y < jpg.MCU_rows; ++mcu_y) { for (int mcu_x = 0; mcu_x < jpg.MCU_cols; ++mcu_x) { for (int iy = 0; iy < c.v_samp_factor; ++iy) { for (int ix = 0; ix < c.h_samp_factor; ++ix) { int block_y = mcu_y * c.v_samp_factor + iy; int block_x = mcu_x * c.h_samp_factor + ix; int block_idx = block_y * c.width_in_blocks + block_x; coeff_t dc_coeff = c.coeffs[block_idx << 6]; int diff = std::abs(dc_coeff - last_dc_coeff); int nbits = Log2Floor(diff) + 1; dc_histogram->Add(nbits); last_dc_coeff = dc_coeff; } } } } } } void BuildACHistograms(const JPEGData& jpg, JpegHistogram* histo) { for (size_t i = 0; i < jpg.components.size(); ++i) { const JPEGComponent& c = jpg.components[i]; JpegHistogram* ac_histogram = &histo[i]; for (size_t j = 0; j < c.coeffs.size(); j += kDCTBlockSize) { UpdateACHistogramForDCTBlock(&c.coeffs[j], ac_histogram); } } } // Size of everything except the Huffman codes and the entropy coded data. size_t JpegHeaderSize(const JPEGData& jpg, bool strip_metadata) { size_t num_bytes = 0; num_bytes += 2; // SOI if (strip_metadata) { num_bytes += 18; // APP0 } else { for (size_t i = 0; i < jpg.app_data.size(); ++i) { num_bytes += 1 + jpg.app_data[i].size(); } for (size_t i = 0; i < jpg.com_data.size(); ++i) { num_bytes += 2 + jpg.com_data[i].size(); } } // DQT num_bytes += 4; for (size_t i = 0; i < jpg.quant.size(); ++i) { num_bytes += 1 + (jpg.quant[i].precision ? 2 : 1) * kDCTBlockSize; } num_bytes += 10 + 3 * jpg.components.size(); // SOF num_bytes += 4; // DHT (w/o actual Huffman code data) num_bytes += 8 + 2 * jpg.components.size(); // SOS num_bytes += 2; // EOI num_bytes += jpg.tail_data.size(); return num_bytes; } size_t ClusterHistograms(JpegHistogram* histo, size_t* num, int* histo_indexes, uint8_t* depth) { memset(depth, 0, *num * JpegHistogram::kSize); size_t costs[kMaxComponents]; for (size_t i = 0; i < *num; ++i) { histo_indexes[i] = i; std::vector tree(2 * JpegHistogram::kSize + 1); CreateHuffmanTree(histo[i].counts, JpegHistogram::kSize, kJpegHuffmanMaxBitLength, &tree[0], &depth[i * JpegHistogram::kSize]); costs[i] = (HistogramHeaderCost(histo[i]) + HistogramEntropyCost(histo[i], &depth[i * JpegHistogram::kSize])); } const size_t orig_num = *num; while (*num > 1) { size_t last = *num - 1; size_t second_last = *num - 2; JpegHistogram combined(histo[last]); combined.AddHistogram(histo[second_last]); std::vector tree(2 * JpegHistogram::kSize + 1); uint8_t depth_combined[JpegHistogram::kSize] = { 0 }; CreateHuffmanTree(combined.counts, JpegHistogram::kSize, kJpegHuffmanMaxBitLength, &tree[0], depth_combined); size_t cost_combined = (HistogramHeaderCost(combined) + HistogramEntropyCost(combined, depth_combined)); if (cost_combined < costs[last] + costs[second_last]) { histo[second_last] = combined; histo[last] = JpegHistogram(); costs[second_last] = cost_combined; memcpy(&depth[second_last * JpegHistogram::kSize], depth_combined, sizeof(depth_combined)); for (size_t i = 0; i < orig_num; ++i) { if (histo_indexes[i] == last) { histo_indexes[i] = second_last; } } --(*num); } else { break; } } size_t total_cost = 0; for (size_t i = 0; i < *num; ++i) { total_cost += costs[i]; } return (total_cost + 7) / 8; } size_t EstimateJpegDataSize(const int num_components, const std::vector& histograms) { assert(histograms.size() == 2 * num_components); std::vector clustered = histograms; size_t num_dc = num_components; size_t num_ac = num_components; int indexes[kMaxComponents]; uint8_t depth[kMaxComponents * JpegHistogram::kSize]; return (ClusterHistograms(&clustered[0], &num_dc, indexes, depth) + ClusterHistograms(&clustered[num_components], &num_ac, indexes, depth)); } namespace { // Writes DHT and SOS marker segments to out and fills in DC/AC Huffman tables // for each component of the image. bool BuildAndEncodeHuffmanCodes(const JPEGData& jpg, JPEGOutput out, std::vector* dc_huff_tables, std::vector* ac_huff_tables) { const int ncomps = jpg.components.size(); dc_huff_tables->resize(ncomps); ac_huff_tables->resize(ncomps); // Build separate DC histograms for each component. std::vector histograms(ncomps); BuildDCHistograms(jpg, &histograms[0]); // Cluster DC histograms. size_t num_dc_histo = ncomps; int dc_histo_indexes[kMaxComponents]; std::vector depths(ncomps * JpegHistogram::kSize); ClusterHistograms(&histograms[0], &num_dc_histo, dc_histo_indexes, &depths[0]); // Build separate AC histograms for each component. histograms.resize(num_dc_histo + ncomps); depths.resize((num_dc_histo + ncomps) * JpegHistogram::kSize); BuildACHistograms(jpg, &histograms[num_dc_histo]); // Cluster AC histograms. size_t num_ac_histo = ncomps; int ac_histo_indexes[kMaxComponents]; ClusterHistograms(&histograms[num_dc_histo], &num_ac_histo, ac_histo_indexes, &depths[num_dc_histo * JpegHistogram::kSize]); // Compute DHT and SOS marker data sizes and start emitting DHT marker. int num_histo = num_dc_histo + num_ac_histo; histograms.resize(num_histo); int total_count = 0; for (size_t i = 0; i < histograms.size(); ++i) { total_count += histograms[i].NumSymbols(); } const size_t dht_marker_len = 2 + num_histo * (kJpegHuffmanMaxBitLength + 1) + total_count; const size_t sos_marker_len = 6 + 2 * ncomps; std::vector data(dht_marker_len + sos_marker_len + 4); size_t pos = 0; data[pos++] = 0xff; data[pos++] = 0xc4; data[pos++] = static_cast(dht_marker_len >> 8); data[pos++] = dht_marker_len & 0xff; // Compute Huffman codes for each histograms. for (int i = 0; i < num_histo; ++i) { const bool is_dc = static_cast(i) < num_dc_histo; const int idx = is_dc ? i : i - num_dc_histo; int counts[kJpegHuffmanMaxBitLength + 1] = { 0 }; int values[JpegHistogram::kSize] = { 0 }; BuildHuffmanCode(&depths[i * JpegHistogram::kSize], counts, values); HuffmanCodeTable table; for (int j = 0; j < 256; ++j) table.depth[j] = 255; BuildHuffmanCodeTable(counts, values, &table); for (int c = 0; c < ncomps; ++c) { if (is_dc) { if (dc_histo_indexes[c] == idx) (*dc_huff_tables)[c] = table; } else { if (ac_histo_indexes[c] == idx) (*ac_huff_tables)[c] = table; } } int max_length = kJpegHuffmanMaxBitLength; while (max_length > 0 && counts[max_length] == 0) --max_length; --counts[max_length]; int total_count = 0; for (int j = 0; j <= max_length; ++j) total_count += counts[j]; data[pos++] = is_dc ? i : static_cast(i - num_dc_histo + 0x10); for (size_t j = 1; j <= kJpegHuffmanMaxBitLength; ++j) { data[pos++] = counts[j]; } for (int j = 0; j < total_count; ++j) { data[pos++] = values[j]; } } // Emit SOS marker data. data[pos++] = 0xff; data[pos++] = 0xda; data[pos++] = static_cast(sos_marker_len >> 8); data[pos++] = sos_marker_len & 0xff; data[pos++] = ncomps; for (int i = 0; i < ncomps; ++i) { data[pos++] = jpg.components[i].id; data[pos++] = (dc_histo_indexes[i] << 4) | ac_histo_indexes[i]; } data[pos++] = 0; data[pos++] = 63; data[pos++] = 0; assert(pos == data.size()); return JPEGWrite(out, &data[0], data.size()); } void EncodeDCTBlockSequential(const coeff_t* coeffs, const HuffmanCodeTable& dc_huff, const HuffmanCodeTable& ac_huff, coeff_t* last_dc_coeff, BitWriter* bw) { coeff_t temp2; coeff_t temp; temp2 = coeffs[0]; temp = temp2 - *last_dc_coeff; *last_dc_coeff = temp2; temp2 = temp; if (temp < 0) { temp = -temp; temp2--; } int nbits = Log2Floor(temp) + 1; bw->WriteBits(dc_huff.depth[nbits], dc_huff.code[nbits]); if (nbits > 0) { bw->WriteBits(nbits, temp2 & ((1 << nbits) - 1)); } int r = 0; for (int k = 1; k < 64; ++k) { if ((temp = coeffs[kJPEGNaturalOrder[k]]) == 0) { r++; continue; } if (temp < 0) { temp = -temp; temp2 = ~temp; } else { temp2 = temp; } while (r > 15) { bw->WriteBits(ac_huff.depth[0xf0], ac_huff.code[0xf0]); r -= 16; } int nbits = Log2FloorNonZero(temp) + 1; int symbol = (r << 4) + nbits; bw->WriteBits(ac_huff.depth[symbol], ac_huff.code[symbol]); bw->WriteBits(nbits, temp2 & ((1 << nbits) - 1)); r = 0; } if (r > 0) { bw->WriteBits(ac_huff.depth[0], ac_huff.code[0]); } } bool EncodeScan(const JPEGData& jpg, const std::vector& dc_huff_table, const std::vector& ac_huff_table, JPEGOutput out) { coeff_t last_dc_coeff[kMaxComponents] = { 0 }; BitWriter bw(1 << 17); for (int mcu_y = 0; mcu_y < jpg.MCU_rows; ++mcu_y) { for (int mcu_x = 0; mcu_x < jpg.MCU_cols; ++mcu_x) { // Encode one MCU for (size_t i = 0; i < jpg.components.size(); ++i) { const JPEGComponent& c = jpg.components[i]; int nblocks_y = c.v_samp_factor; int nblocks_x = c.h_samp_factor; for (int iy = 0; iy < nblocks_y; ++iy) { for (int ix = 0; ix < nblocks_x; ++ix) { int block_y = mcu_y * nblocks_y + iy; int block_x = mcu_x * nblocks_x + ix; int block_idx = block_y * c.width_in_blocks + block_x; const coeff_t* coeffs = &c.coeffs[block_idx << 6]; EncodeDCTBlockSequential(coeffs, dc_huff_table[i], ac_huff_table[i], &last_dc_coeff[i], &bw); } } } if (bw.pos > (1 << 16)) { if (!JPEGWrite(out, bw.data.get(), bw.pos)) { return false; } bw.pos = 0; } } } bw.JumpToByteBoundary(); return !bw.overflow && JPEGWrite(out, bw.data.get(), bw.pos); } } // namespace bool WriteJpeg(const JPEGData& jpg, bool strip_metadata, JPEGOutput out) { static const uint8_t kSOIMarker[2] = { 0xff, 0xd8 }; static const uint8_t kEOIMarker[2] = { 0xff, 0xd9 }; std::vector dc_codes; std::vector ac_codes; return (JPEGWrite(out, kSOIMarker, sizeof(kSOIMarker)) && EncodeMetadata(jpg, strip_metadata, out) && EncodeDQT(jpg.quant, out) && EncodeSOF(jpg, out) && BuildAndEncodeHuffmanCodes(jpg, out, &dc_codes, &ac_codes) && EncodeScan(jpg, dc_codes, ac_codes, out) && JPEGWrite(out, kEOIMarker, sizeof(kEOIMarker)) && (strip_metadata || JPEGWrite(out, jpg.tail_data))); } int NullOut(void* data, const uint8_t* buf, size_t count) { return count; } void BuildSequentialHuffmanCodes( const JPEGData& jpg, std::vector* dc_huffman_code_tables, std::vector* ac_huffman_code_tables) { JPEGOutput out(NullOut, nullptr); BuildAndEncodeHuffmanCodes(jpg, out, dc_huffman_code_tables, ac_huffman_code_tables); } } // namespace guetzli ================================================ FILE: guetzli/jpeg_data_writer.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Functions for writing a JPEGData object into a jpeg byte stream. #ifndef GUETZLI_JPEG_DATA_WRITER_H_ #define GUETZLI_JPEG_DATA_WRITER_H_ #include #include #include #include "guetzli/jpeg_data.h" namespace guetzli { // Function pointer type used to write len bytes into buf. Returns the // number of bytes written or -1 on error. typedef int (*JPEGOutputHook)(void* data, const uint8_t* buf, size_t len); // Output callback function with associated data. struct JPEGOutput { JPEGOutput(JPEGOutputHook cb, void* data) : cb(cb), data(data) {} bool Write(const uint8_t* buf, size_t len) const { return (len == 0) || (cb(data, buf, len) == len); } private: JPEGOutputHook cb; void* data; }; bool WriteJpeg(const JPEGData& jpg, bool strip_metadata, JPEGOutput out); struct HuffmanCodeTable { uint8_t depth[256]; int code[256]; }; void BuildSequentialHuffmanCodes( const JPEGData& jpg, std::vector* dc_huffman_code_tables, std::vector* ac_huffman_code_tables); struct JpegHistogram { static const int kSize = kJpegHuffmanAlphabetSize + 1; JpegHistogram() { Clear(); } void Clear() { memset(counts, 0, sizeof(counts)); counts[kSize - 1] = 1; } void Add(int symbol) { counts[symbol] += 2; } void Add(int symbol, int weight) { counts[symbol] += 2 * weight; } void AddHistogram(const JpegHistogram& other) { for (int i = 0; i + 1 < kSize; ++i) { counts[i] += other.counts[i]; } counts[kSize - 1] = 1; } int NumSymbols() const { int n = 0; for (int i = 0; i + 1 < kSize; ++i) { n += (counts[i] > 0 ? 1 : 0); } return n; } uint32_t counts[kSize]; }; void BuildDCHistograms(const JPEGData& jpg, JpegHistogram* histo); void BuildACHistograms(const JPEGData& jpg, JpegHistogram* histo); size_t JpegHeaderSize(const JPEGData& jpg, bool strip_metadata); size_t EstimateJpegDataSize(const int num_components, const std::vector& histograms); size_t HistogramEntropyCost(const JpegHistogram& histo, const uint8_t depths[256]); size_t HistogramHeaderCost(const JpegHistogram& histo); void UpdateACHistogramForDCTBlock(const coeff_t* coeffs, JpegHistogram* ac_histogram); size_t ClusterHistograms(JpegHistogram* histo, size_t* num, int* histo_indexes, uint8_t* depths); } // namespace guetzli #endif // GUETZLI_JPEG_DATA_WRITER_H_ ================================================ FILE: guetzli/jpeg_error.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Definition of error codes for parsing jpeg files. #ifndef GUETZLI_JPEG_ERROR_H_ #define GUETZLI_JPEG_ERROR_H_ namespace guetzli { enum JPEGReadError { JPEG_OK = 0, JPEG_SOI_NOT_FOUND, JPEG_SOF_NOT_FOUND, JPEG_UNEXPECTED_EOF, JPEG_MARKER_BYTE_NOT_FOUND, JPEG_UNSUPPORTED_MARKER, JPEG_WRONG_MARKER_SIZE, JPEG_INVALID_PRECISION, JPEG_INVALID_WIDTH, JPEG_INVALID_HEIGHT, JPEG_INVALID_NUMCOMP, JPEG_INVALID_SAMP_FACTOR, JPEG_INVALID_START_OF_SCAN, JPEG_INVALID_END_OF_SCAN, JPEG_INVALID_SCAN_BIT_POSITION, JPEG_INVALID_COMPS_IN_SCAN, JPEG_INVALID_HUFFMAN_INDEX, JPEG_INVALID_QUANT_TBL_INDEX, JPEG_INVALID_QUANT_VAL, JPEG_INVALID_MARKER_LEN, JPEG_INVALID_SAMPLING_FACTORS, JPEG_INVALID_HUFFMAN_CODE, JPEG_INVALID_SYMBOL, JPEG_NON_REPRESENTABLE_DC_COEFF, JPEG_NON_REPRESENTABLE_AC_COEFF, JPEG_INVALID_SCAN, JPEG_OVERLAPPING_SCANS, JPEG_INVALID_SCAN_ORDER, JPEG_EXTRA_ZERO_RUN, JPEG_DUPLICATE_DRI, JPEG_DUPLICATE_SOF, JPEG_WRONG_RESTART_MARKER, JPEG_DUPLICATE_COMPONENT_ID, JPEG_COMPONENT_NOT_FOUND, JPEG_HUFFMAN_TABLE_NOT_FOUND, JPEG_HUFFMAN_TABLE_ERROR, JPEG_QUANT_TABLE_NOT_FOUND, JPEG_EMPTY_DHT, JPEG_EMPTY_DQT, JPEG_OUT_OF_BAND_COEFF, JPEG_EOB_RUN_TOO_LONG, JPEG_IMAGE_TOO_LARGE, }; } // namespace guetzli #endif // GUETZLI_JPEG_ERROR_H_ ================================================ FILE: guetzli/jpeg_huffman_decode.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/jpeg_huffman_decode.h" #include #include #include #include #include "guetzli/jpeg_data.h" namespace guetzli { // Returns the table width of the next 2nd level table, count is the histogram // of bit lengths for the remaining symbols, len is the code length of the next // processed symbol. static inline int NextTableBitSize(const int* count, int len) { int left = 1 << (len - kJpegHuffmanRootTableBits); while (len < kJpegHuffmanMaxBitLength) { left -= count[len]; if (left <= 0) break; ++len; left <<= 1; } return len - kJpegHuffmanRootTableBits; } int BuildJpegHuffmanTable(const int* count_in, const int* symbols, HuffmanTableEntry* lut) { HuffmanTableEntry code; // current table entry HuffmanTableEntry* table; // next available space in table int len; // current code length int idx; // symbol index int key; // prefix code int reps; // number of replicate key values in current table int low; // low bits for current root entry int table_bits; // key length of current table int table_size; // size of current table int total_size; // sum of root table size and 2nd level table sizes // Make a local copy of the input bit length histogram. int count[kJpegHuffmanMaxBitLength + 1] = { 0 }; int total_count = 0; for (len = 1; len <= kJpegHuffmanMaxBitLength; ++len) { count[len] = count_in[len]; total_count += count[len]; } table = lut; table_bits = kJpegHuffmanRootTableBits; table_size = 1 << table_bits; total_size = table_size; // Special case code with only one value. if (total_count == 1) { code.bits = 0; code.value = symbols[0]; for (key = 0; key < total_size; ++key) { table[key] = code; } return total_size; } // Fill in root table. key = 0; idx = 0; for (len = 1; len <= kJpegHuffmanRootTableBits; ++len) { for (; count[len] > 0; --count[len]) { code.bits = len; code.value = symbols[idx++]; reps = 1 << (kJpegHuffmanRootTableBits - len); while (reps--) { table[key++] = code; } } } // Fill in 2nd level tables and add pointers to root table. table += table_size; table_size = 0; low = 0; for (len = kJpegHuffmanRootTableBits + 1; len <= kJpegHuffmanMaxBitLength; ++len) { for (; count[len] > 0; --count[len]) { // Start a new sub-table if the previous one is full. if (low >= table_size) { table += table_size; table_bits = NextTableBitSize(count, len); table_size = 1 << table_bits; total_size += table_size; low = 0; lut[key].bits = table_bits + kJpegHuffmanRootTableBits; lut[key].value = (table - lut) - key; ++key; } code.bits = len - kJpegHuffmanRootTableBits; code.value = symbols[idx++]; reps = 1 << (table_bits - code.bits); while (reps--) { table[low++] = code; } } } return total_size; } } // namespace guetzli ================================================ FILE: guetzli/jpeg_huffman_decode.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Utility function for building a Huffman lookup table for the jpeg decoder. #ifndef GUETZLI_JPEG_HUFFMAN_DECODE_H_ #define GUETZLI_JPEG_HUFFMAN_DECODE_H_ #include namespace guetzli { static const int kJpegHuffmanRootTableBits = 8; // Maximum huffman lookup table size. // According to zlib/examples/enough.c, 758 entries are always enough for // an alphabet of 257 symbols (256 + 1 special symbol for the all 1s code) and // max bit length 16 if the root table has 8 bits. static const int kJpegHuffmanLutSize = 758; struct HuffmanTableEntry { // Initialize the value to an invalid symbol so that we can recognize it // when reading the bit stream using a Huffman code with space > 0. HuffmanTableEntry() : bits(0), value(0xffff) {} uint8_t bits; // number of bits used for this symbol uint16_t value; // symbol value or table offset }; // Builds jpeg-style Huffman lookup table from the given symbols. // The symbols are in order of increasing bit lengths. The number of symbols // with bit length n is given in counts[n] for each n >= 1. // Returns the size of the lookup table. int BuildJpegHuffmanTable(const int* counts, const int* symbols, HuffmanTableEntry* lut); } // namespace guetzli #endif // GUETZLI_JPEG_HUFFMAN_DECODE_H_ ================================================ FILE: guetzli/order.inc ================================================ // Automatically generated by guetzli/order:update_c_code static const float csf[192] = { 0.0f, 1.71014f, 0.298711f, 0.233709f, 0.223126f, 0.207072f, 0.192775f, 0.161201f, 2.05807f, 0.222927f, 0.203406f, 0.188465f, 0.184668f, 0.169993f, 0.159142f, 0.130155f, 0.430518f, 0.204939f, 0.206655f, 0.192231f, 0.182941f, 0.169455f, 0.157599f, 0.127153f, 0.234757f, 0.191098f, 0.192698f, 0.17425f, 0.166503f, 0.142154f, 0.126182f, 0.104196f, 0.226117f, 0.185373f, 0.183825f, 0.166643f, 0.159414f, 0.12636f, 0.108696f, 0.0911974f, 0.207463f, 0.171517f, 0.170124f, 0.141582f, 0.126213f, 0.103627f, 0.0882436f, 0.0751848f, 0.196436f, 0.161947f, 0.159271f, 0.126938f, 0.109125f, 0.0878027f, 0.0749842f, 0.0633859f, 0.165232f, 0.132905f, 0.128679f, 0.105766f, 0.0906087f, 0.0751544f, 0.0641187f, 0.0529921f, 0.0f, 0.147235f, 0.11264f, 0.0757892f, 0.0493929f, 0.0280663f, 0.0075012f, -0.000945567f, 0.149251f, 0.0964806f, 0.0786224f, 0.05206f, 0.0292758f, 0.00353094f, -0.00277912f, -0.00404481f, 0.115551f, 0.0793142f, 0.0623735f, 0.0405019f, 0.0152656f, -0.00145742f, -0.00370369f, -0.00375106f, 0.0791547f, 0.0537506f, 0.0413634f, 0.0193486f, 0.000609066f, -0.00510923f, -0.0046452f, -0.00385187f, 0.0544534f, 0.0334066f, 0.0153899f, 0.000539088f, -0.00356085f, -0.00535661f, -0.00429145f, -0.00343131f, 0.0356439f, 0.00865645f, 0.00165229f, -0.00425931f, -0.00507324f, -0.00459083f, -0.003703f, -0.00310327f, 0.0121926f, -0.0009259f, -0.00330991f, -0.00499378f, -0.00437381f, -0.00377427f, -0.00311731f, -0.00255125f, -0.000320593f, -0.00426043f, -0.00416549f, -0.00419364f, -0.00365418f, -0.00317499f, -0.00255932f, -0.00217917f, 0.0f, 0.143471f, 0.124336f, 0.0947465f, 0.0814066f, 0.0686776f, 0.0588122f, 0.0374415f, 0.146315f, 0.105334f, 0.0949415f, 0.0784241f, 0.0689064f, 0.0588304f, 0.0495961f, 0.0202342f, 0.123818f, 0.0952654f, 0.0860556f, 0.0724158f, 0.0628307f, 0.0529965f, 0.0353941f, 0.00815821f, 0.097054f, 0.080422f, 0.0731085f, 0.0636154f, 0.055606f, 0.0384127f, 0.0142879f, 0.00105195f, 0.0849312f, 0.071115f, 0.0631183f, 0.0552972f, 0.0369221f, 0.00798314f, 0.000716374f, -0.00200948f, 0.0722298f, 0.0599559f, 0.054841f, 0.0387529f, 0.0107262f, 0.000355315f, -0.00244803f, -0.00335222f, 0.0635335f, 0.0514196f, 0.0406309f, 0.0125833f, 0.00151305f, -0.00140269f, -0.00362547f, -0.00337649f, 0.0472024f, 0.0198725f, 0.0113437f, 0.00266305f, -0.00137183f, -0.00354158f, -0.00341292f, -0.00290074f }; static const float bias[192] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0 }; ================================================ FILE: guetzli/output_image.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/output_image.h" #include #include #include #include #include #include #include "guetzli/idct.h" #include "guetzli/color_transform.h" #include "guetzli/dct_double.h" #include "guetzli/gamma_correct.h" #include "guetzli/preprocess_downsample.h" #include "guetzli/quantize.h" namespace guetzli { OutputImageComponent::OutputImageComponent(int w, int h) : width_(w), height_(h) { Reset(1, 1); } void OutputImageComponent::Reset(int factor_x, int factor_y) { factor_x_ = factor_x; factor_y_ = factor_y; width_in_blocks_ = (width_ + 8 * factor_x_ - 1) / (8 * factor_x_); height_in_blocks_ = (height_ + 8 * factor_y_ - 1) / (8 * factor_y_); num_blocks_ = width_in_blocks_ * height_in_blocks_; coeffs_ = std::vector(num_blocks_ * kDCTBlockSize); pixels_ = std::vector(width_ * height_, 128 << 4); for (int i = 0; i < kDCTBlockSize; ++i) quant_[i] = 1; } bool OutputImageComponent::IsAllZero() const { int numcoeffs = num_blocks_ * kDCTBlockSize; for (int i = 0; i < numcoeffs; ++i) { if (coeffs_[i] != 0) return false; } return true; } void OutputImageComponent::GetCoeffBlock(int block_x, int block_y, coeff_t block[kDCTBlockSize]) const { assert(block_x < width_in_blocks_); assert(block_y < height_in_blocks_); int offset = (block_y * width_in_blocks_ + block_x) * kDCTBlockSize; memcpy(block, &coeffs_[offset], kDCTBlockSize * sizeof(coeffs_[0])); } void OutputImageComponent::ToPixels(int xmin, int ymin, int xsize, int ysize, uint8_t* out, int stride) const { assert(xmin >= 0); assert(ymin >= 0); assert(xmin < width_); assert(ymin < height_); const int yend1 = ymin + ysize; const int yend0 = std::min(yend1, height_); int y = ymin; for (; y < yend0; ++y) { const int xend1 = xmin + xsize; const int xend0 = std::min(xend1, width_); int x = xmin; int px = y * width_ + xmin; for (; x < xend0; ++x, ++px, out += stride) { *out = static_cast((pixels_[px] + 8 - (x & 1)) >> 4); } const int offset = -stride; for (; x < xend1; ++x) { *out = out[offset]; out += stride; } } for (; y < yend1; ++y) { const int offset = -stride * xsize; for (int x = 0; x < xsize; ++x) { *out = out[offset]; out += stride; } } } void OutputImageComponent::ToFloatPixels(float* out, int stride) const { assert(factor_x_ == 1); assert(factor_y_ == 1); for (int block_y = 0; block_y < height_in_blocks_; ++block_y) { for (int block_x = 0; block_x < width_in_blocks_; ++block_x) { coeff_t block[kDCTBlockSize]; GetCoeffBlock(block_x, block_y, block); double blockd[kDCTBlockSize]; for (int k = 0; k < kDCTBlockSize; ++k) { blockd[k] = block[k]; } ComputeBlockIDCTDouble(blockd); for (int iy = 0; iy < 8; ++iy) { for (int ix = 0; ix < 8; ++ix) { int y = block_y * 8 + iy; int x = block_x * 8 + ix; if (y >= height_ || x >= width_) continue; out[(y * width_ + x) * stride] = static_cast(blockd[8 * iy + ix] + 128.0); } } } } } void OutputImageComponent::SetCoeffBlock(int block_x, int block_y, const coeff_t block[kDCTBlockSize]) { assert(block_x < width_in_blocks_); assert(block_y < height_in_blocks_); int offset = (block_y * width_in_blocks_ + block_x) * kDCTBlockSize; memcpy(&coeffs_[offset], block, kDCTBlockSize * sizeof(coeffs_[0])); uint8_t idct[kDCTBlockSize]; ComputeBlockIDCT(&coeffs_[offset], idct); UpdatePixelsForBlock(block_x, block_y, idct); } void OutputImageComponent::UpdatePixelsForBlock( int block_x, int block_y, const uint8_t idct[kDCTBlockSize]) { if (factor_x_ == 1 && factor_y_ == 1) { for (int iy = 0; iy < 8; ++iy) { for (int ix = 0; ix < 8; ++ix) { int x = 8 * block_x + ix; int y = 8 * block_y + iy; if (x >= width_ || y >= height_) continue; int p = y * width_ + x; pixels_[p] = idct[8 * iy + ix] << 4; } } } else if (factor_x_ == 2 && factor_y_ == 2) { // Fill in the 10x10 pixel area in the subsampled image that will be the // basis of the upsampling. This area is enough to hold the 3x3 kernel of // the fancy upsampler around each pixel. static const int kSubsampledEdgeSize = 10; uint16_t subsampled[kSubsampledEdgeSize * kSubsampledEdgeSize]; for (int j = 0; j < kSubsampledEdgeSize; ++j) { // The order we fill in the rows is: // 8 rows intersecting the block, row below, row above const int y0 = block_y * 16 + (j < 9 ? j * 2 : -2); for (int i = 0; i < kSubsampledEdgeSize; ++i) { // The order we fill in each row is: // 8 pixels within the block, left edge, right edge const int ix = ((j < 9 ? (j + 1) * kSubsampledEdgeSize : 0) + (i < 9 ? i + 1 : 0)); const int x0 = block_x * 16 + (i < 9 ? i * 2 : -2); if (x0 < 0) { subsampled[ix] = subsampled[ix + 1]; } else if (y0 < 0) { subsampled[ix] = subsampled[ix + kSubsampledEdgeSize]; } else if (x0 >= width_) { subsampled[ix] = subsampled[ix - 1]; } else if (y0 >= height_) { subsampled[ix] = subsampled[ix - kSubsampledEdgeSize]; } else if (i < 8 && j < 8) { subsampled[ix] = idct[j * 8 + i] << 4; } else { // Reconstruct the subsampled pixels around the edge of the current // block by computing the inverse of the fancy upsampler. const int y1 = std::max(y0 - 1, 0); const int x1 = std::max(x0 - 1, 0); subsampled[ix] = (pixels_[y0 * width_ + x0] * 9 + pixels_[y1 * width_ + x1] + pixels_[y0 * width_ + x1] * -3 + pixels_[y1 * width_ + x0] * -3) >> 2; } } } // Determine area to update. int xmin = std::max(block_x * 16 - 1, 0); int xmax = std::min(block_x * 16 + 16, width_ - 1); int ymin = std::max(block_y * 16 - 1, 0); int ymax = std::min(block_y * 16 + 16, height_ - 1); // Apply the fancy upsampler on the subsampled block. for (int y = ymin; y <= ymax; ++y) { const int y0 = ((y & ~1) / 2 - block_y * 8 + 1) * kSubsampledEdgeSize; const int dy = ((y & 1) * 2 - 1) * kSubsampledEdgeSize; uint16_t* rowptr = &pixels_[y * width_]; for (int x = xmin; x <= xmax; ++x) { const int x0 = (x & ~1) / 2 - block_x * 8 + 1; const int dx = (x & 1) * 2 - 1; const int ix = x0 + y0; rowptr[x] = (subsampled[ix] * 9 + subsampled[ix + dy] * 3 + subsampled[ix + dx] * 3 + subsampled[ix + dx + dy]) >> 4; } } } else { printf("Sampling ratio not supported: factor_x = %d factor_y = %d\n", factor_x_, factor_y_); exit(1); } } void OutputImageComponent::CopyFromJpegComponent(const JPEGComponent& comp, int factor_x, int factor_y, const int* quant) { Reset(factor_x, factor_y); assert(width_in_blocks_ <= comp.width_in_blocks); assert(height_in_blocks_ <= comp.height_in_blocks); const size_t src_row_size = comp.width_in_blocks * kDCTBlockSize; for (int block_y = 0; block_y < height_in_blocks_; ++block_y) { const coeff_t* src_coeffs = &comp.coeffs[block_y * src_row_size]; for (int block_x = 0; block_x < width_in_blocks_; ++block_x) { coeff_t block[kDCTBlockSize]; for (int i = 0; i < kDCTBlockSize; ++i) { block[i] = src_coeffs[i] * quant[i]; } SetCoeffBlock(block_x, block_y, block); src_coeffs += kDCTBlockSize; } } memcpy(quant_, quant, sizeof(quant_)); } void OutputImageComponent::ApplyGlobalQuantization(const int q[kDCTBlockSize]) { for (int block_y = 0; block_y < height_in_blocks_; ++block_y) { for (int block_x = 0; block_x < width_in_blocks_; ++block_x) { coeff_t block[kDCTBlockSize]; GetCoeffBlock(block_x, block_y, block); if (QuantizeBlock(block, q)) { SetCoeffBlock(block_x, block_y, block); } } } memcpy(quant_, q, sizeof(quant_)); } OutputImage::OutputImage(int w, int h) : width_(w), height_(h), components_(3, OutputImageComponent(w, h)) {} void OutputImage::CopyFromJpegData(const JPEGData& jpg) { for (size_t i = 0; i < jpg.components.size(); ++i) { const JPEGComponent& comp = jpg.components[i]; assert(jpg.max_h_samp_factor % comp.h_samp_factor == 0); assert(jpg.max_v_samp_factor % comp.v_samp_factor == 0); int factor_x = jpg.max_h_samp_factor / comp.h_samp_factor; int factor_y = jpg.max_v_samp_factor / comp.v_samp_factor; assert(comp.quant_idx < jpg.quant.size()); components_[i].CopyFromJpegComponent(comp, factor_x, factor_y, &jpg.quant[comp.quant_idx].values[0]); } } namespace { void SetDownsampledCoefficients(const std::vector& pixels, int factor_x, int factor_y, OutputImageComponent* comp) { assert(pixels.size() == comp->width() * comp->height()); comp->Reset(factor_x, factor_y); for (int block_y = 0; block_y < comp->height_in_blocks(); ++block_y) { for (int block_x = 0; block_x < comp->width_in_blocks(); ++block_x) { double blockd[kDCTBlockSize]; int x0 = 8 * block_x * factor_x; int y0 = 8 * block_y * factor_y; assert(x0 < comp->width()); assert(y0 < comp->height()); for (int iy = 0; iy < 8; ++iy) { for (int ix = 0; ix < 8; ++ix) { float avg = 0.0; for (int j = 0; j < factor_y; ++j) { for (int i = 0; i < factor_x; ++i) { int x = std::min(x0 + ix * factor_x + i, comp->width() - 1); int y = std::min(y0 + iy * factor_y + j, comp->height() - 1); avg += pixels[y * comp->width() + x]; } } avg /= factor_x * factor_y; blockd[iy * 8 + ix] = avg; } } ComputeBlockDCTDouble(blockd); blockd[0] -= 1024.0; coeff_t block[kDCTBlockSize]; for (int k = 0; k < kDCTBlockSize; ++k) { block[k] = static_cast(std::round(blockd[k])); } comp->SetCoeffBlock(block_x, block_y, block); } } } } // namespace void OutputImage::Downsample(const DownsampleConfig& cfg) { if (components_[1].IsAllZero() && components_[2].IsAllZero()) { // If the image is already grayscale, nothing to do. return; } if (cfg.use_silver_screen && cfg.u_factor_x == 2 && cfg.u_factor_y == 2 && cfg.v_factor_x == 2 && cfg.v_factor_y == 2) { std::vector rgb = ToSRGB(); std::vector > yuv = RGBToYUV420(rgb, width_, height_); SetDownsampledCoefficients(yuv[0], 1, 1, &components_[0]); SetDownsampledCoefficients(yuv[1], 2, 2, &components_[1]); SetDownsampledCoefficients(yuv[2], 2, 2, &components_[2]); return; } // Get the floating-point precision YUV array represented by the set of // DCT coefficients. std::vector > yuv(3, std::vector(width_ * height_)); for (int c = 0; c < 3; ++c) { components_[c].ToFloatPixels(&yuv[c][0], 1); } yuv = PreProcessChannel(width_, height_, 2, 1.3f, 0.5f, cfg.u_sharpen, cfg.u_blur, yuv); yuv = PreProcessChannel(width_, height_, 1, 1.3f, 0.5f, cfg.v_sharpen, cfg.v_blur, yuv); // Do the actual downsampling (averaging) and forward-DCT. if (cfg.u_factor_x != 1 || cfg.u_factor_y != 1) { SetDownsampledCoefficients(yuv[1], cfg.u_factor_x, cfg.u_factor_y, &components_[1]); } if (cfg.v_factor_x != 1 || cfg.v_factor_y != 1) { SetDownsampledCoefficients(yuv[2], cfg.v_factor_x, cfg.v_factor_y, &components_[2]); } } void OutputImage::ApplyGlobalQuantization(const int q[3][kDCTBlockSize]) { for (int c = 0; c < 3; ++c) { components_[c].ApplyGlobalQuantization(&q[c][0]); } } void OutputImage::SaveToJpegData(JPEGData* jpg) const { assert(components_[0].factor_x() == 1); assert(components_[0].factor_y() == 1); jpg->width = width_; jpg->height = height_; jpg->max_h_samp_factor = 1; jpg->max_v_samp_factor = 1; jpg->MCU_cols = components_[0].width_in_blocks(); jpg->MCU_rows = components_[0].height_in_blocks(); int ncomp = components_[1].IsAllZero() && components_[2].IsAllZero() ? 1 : 3; for (int i = 1; i < ncomp; ++i) { jpg->max_h_samp_factor = std::max(jpg->max_h_samp_factor, components_[i].factor_x()); jpg->max_v_samp_factor = std::max(jpg->max_h_samp_factor, components_[i].factor_y()); jpg->MCU_cols = std::min(jpg->MCU_cols, components_[i].width_in_blocks()); jpg->MCU_rows = std::min(jpg->MCU_rows, components_[i].height_in_blocks()); } jpg->components.resize(ncomp); int q[3][kDCTBlockSize]; for (int c = 0; c < 3; ++c) { memcpy(&q[c][0], components_[c].quant(), kDCTBlockSize * sizeof(q[0][0])); } for (int c = 0; c < ncomp; ++c) { JPEGComponent* comp = &jpg->components[c]; assert(jpg->max_h_samp_factor % components_[c].factor_x() == 0); assert(jpg->max_v_samp_factor % components_[c].factor_y() == 0); comp->id = c; comp->h_samp_factor = jpg->max_h_samp_factor / components_[c].factor_x(); comp->v_samp_factor = jpg->max_v_samp_factor / components_[c].factor_y(); comp->width_in_blocks = jpg->MCU_cols * comp->h_samp_factor; comp->height_in_blocks = jpg->MCU_rows * comp->v_samp_factor; comp->num_blocks = comp->width_in_blocks * comp->height_in_blocks; comp->coeffs.resize(kDCTBlockSize * comp->num_blocks); int last_dc = 0; const coeff_t* src_coeffs = components_[c].coeffs(); coeff_t* dest_coeffs = &comp->coeffs[0]; for (int block_y = 0; block_y < comp->height_in_blocks; ++block_y) { for (int block_x = 0; block_x < comp->width_in_blocks; ++block_x) { if (block_y >= components_[c].height_in_blocks() || block_x >= components_[c].width_in_blocks()) { dest_coeffs[0] = last_dc; for (int k = 1; k < kDCTBlockSize; ++k) { dest_coeffs[k] = 0; } } else { for (int k = 0; k < kDCTBlockSize; ++k) { const int quant = q[c][k]; int coeff = src_coeffs[k]; assert(coeff % quant == 0); dest_coeffs[k] = coeff / quant; } src_coeffs += kDCTBlockSize; } last_dc = dest_coeffs[0]; dest_coeffs += kDCTBlockSize; } } } SaveQuantTables(q, jpg); } std::vector OutputImage::ToSRGB(int xmin, int ymin, int xsize, int ysize) const { std::vector rgb(xsize * ysize * 3); for (int c = 0; c < 3; ++c) { components_[c].ToPixels(xmin, ymin, xsize, ysize, &rgb[c], 3); } for (size_t p = 0; p < rgb.size(); p += 3) { ColorTransformYCbCrToRGB(&rgb[p]); } return rgb; } std::vector OutputImage::ToSRGB() const { return ToSRGB(0, 0, width_, height_); } void OutputImage::ToLinearRGB(int xmin, int ymin, int xsize, int ysize, std::vector >* rgb) const { const double* lut = Srgb8ToLinearTable(); std::vector rgb_pixels = ToSRGB(xmin, ymin, xsize, ysize); for (int p = 0; p < xsize * ysize; ++p) { for (int i = 0; i < 3; ++i) { (*rgb)[i][p] = static_cast(lut[rgb_pixels[3 * p + i]]); } } } void OutputImage::ToLinearRGB(std::vector >* rgb) const { ToLinearRGB(0, 0, width_, height_, rgb); } std::string OutputImage::FrameTypeStr() const { char buf[128]; int len = snprintf(buf, sizeof(buf), "f%d%d%d%d%d%d", component(0).factor_x(), component(0).factor_y(), component(1).factor_x(), component(1).factor_y(), component(2).factor_x(), component(2).factor_y()); return std::string(buf, len); } } // namespace guetzli ================================================ FILE: guetzli/output_image.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_OUTPUT_IMAGE_H_ #define GUETZLI_OUTPUT_IMAGE_H_ #include #include #include "guetzli/jpeg_data.h" namespace guetzli { class OutputImageComponent { public: OutputImageComponent(int w, int h); void Reset(int factor_x, int factor_y); int width() const { return width_; } int height() const { return height_; } int factor_x() const { return factor_x_; } int factor_y() const { return factor_y_; } int width_in_blocks() const { return width_in_blocks_; } int height_in_blocks() const { return height_in_blocks_; } const coeff_t* coeffs() const { return &coeffs_[0]; } const int* quant() const { return &quant_[0]; } bool IsAllZero() const; // Fills in block[] with the 8x8 coefficient block with block coordinates // (block_x, block_y). // NOTE: If the component is 2x2 subsampled, this corresponds to the 16x16 // pixel area with upper-left corner (16 * block_x, 16 * block_y). void GetCoeffBlock(int block_x, int block_y, coeff_t block[kDCTBlockSize]) const; // Fills in out[] array with the 8-bit pixel view of this component cropped // to the specified window. The window's upper-left corner, (xmin, ymin) must // be within the image, but the window may extend past the image. In that // case the edge pixels are duplicated. void ToPixels(int xmin, int ymin, int xsize, int ysize, uint8_t* out, int stride) const; // Fills in out[] array with the floating-point precision pixel view of the // component. // REQUIRES: factor_x() == 1 and factor_y() == 1. void ToFloatPixels(float* out, int stride) const; // Sets the 8x8 coefficient block with block coordinates (block_x, block_y) // to block[]. // NOTE: If the component is 2x2 subsampled, this corresponds to the 16x16 // pixel area with upper-left corner (16 * block_x, 16 * block_y). // REQUIRES: block[k] % quant()[k] == 0 for each coefficient index k. void SetCoeffBlock(int block_x, int block_y, const coeff_t block[kDCTBlockSize]); // Requires that comp is not downsampled. void CopyFromJpegComponent(const JPEGComponent& comp, int factor_x, int factor_y, const int* quant); void ApplyGlobalQuantization(const int q[kDCTBlockSize]); private: void UpdatePixelsForBlock(int block_x, int block_y, const uint8_t idct[kDCTBlockSize]); const int width_; const int height_; int factor_x_; int factor_y_; int width_in_blocks_; int height_in_blocks_; int num_blocks_; std::vector coeffs_; std::vector pixels_; // Same as last argument of ApplyGlobalQuantization() (default is all 1s). int quant_[kDCTBlockSize]; }; class OutputImage { public: OutputImage(int w, int h); int width() const { return width_; } int height() const { return height_; } OutputImageComponent& component(int c) { return components_[c]; } const OutputImageComponent& component(int c) const { return components_[c]; } // Requires that jpg is in YUV444 format. void CopyFromJpegData(const JPEGData& jpg); void ApplyGlobalQuantization(const int q[3][kDCTBlockSize]); // If sharpen or blur are enabled, preprocesses image before downsampling U or // V to improve butteraugli score and/or reduce file size. // u_sharpen: sharpen the u channel in red areas to improve score (not as // effective as v_sharpen, blue is not so important) // u_blur: blur the u channel in some areas to reduce file size // v_sharpen: sharpen the v channel in red areas to improve score // v_blur: blur the v channel in some areas to reduce file size struct DownsampleConfig { // Default is YUV420. DownsampleConfig() : u_factor_x(2), u_factor_y(2), v_factor_x(2), v_factor_y(2), u_sharpen(true), u_blur(true), v_sharpen(true), v_blur(true), use_silver_screen(false) {} int u_factor_x; int u_factor_y; int v_factor_x; int v_factor_y; bool u_sharpen; bool u_blur; bool v_sharpen; bool v_blur; bool use_silver_screen; }; void Downsample(const DownsampleConfig& cfg); void SaveToJpegData(JPEGData* jpg) const; std::vector ToSRGB() const; std::vector ToSRGB(int xmin, int ymin, int xsize, int ysize) const; void ToLinearRGB(std::vector >* rgb) const; void ToLinearRGB(int xmin, int ymin, int xsize, int ysize, std::vector >* rgb) const; std::string FrameTypeStr() const; private: const int width_; const int height_; std::vector components_; }; } // namespace guetzli #endif // GUETZLI_OUTPUT_IMAGE_H_ ================================================ FILE: guetzli/preprocess_downsample.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/preprocess_downsample.h" #include #include #include #include using std::size_t; namespace { // convolve with size*size kernel std::vector Convolve2D(const std::vector& image, int w, int h, const double* kernel, int size) { auto result = image; int size2 = size / 2; for (size_t i = 0; i < image.size(); i++) { int x = i % w; int y = i / w; // Avoid non-normalized results at boundary by skipping edges. if (x < size2 || x + size - size2 - 1 >= w || y < size2 || y + size - size2 - 1 >= h) { continue; } float v = 0; for (int j = 0; j < size * size; j++) { int x2 = x + j % size - size2; int y2 = y + j / size - size2; v += static_cast(kernel[j]) * image[y2 * w + x2]; } result[i] = v; } return result; } // convolve horizontally and vertically with 1D kernel std::vector Convolve2X(const std::vector& image, int w, int h, const double* kernel, int size, double mul) { auto temp = image; int size2 = size / 2; for (size_t i = 0; i < image.size(); i++) { int x = i % w; int y = i / w; // Avoid non-normalized results at boundary by skipping edges. if (x < size2 || x + size - size2 - 1 >= w) continue; float v = 0; for (int j = 0; j < size; j++) { int x2 = x + j - size2; v += static_cast(kernel[j]) * image[y * w + x2]; } temp[i] = v * static_cast(mul); } auto result = temp; for (size_t i = 0; i < temp.size(); i++) { int x = i % w; int y = i / w; // Avoid non-normalized results at boundary by skipping edges. if (y < size2 || y + size - size2 - 1 >= h) continue; float v = 0; for (int j = 0; j < size; j++) { int y2 = y + j - size2; v += static_cast(kernel[j]) * temp[y2 * w + x]; } result[i] = v * static_cast(mul); } return result; } double Normal(double x, double sigma) { static const double kInvSqrt2Pi = 0.3989422804014327; return std::exp(-x * x / (2 * sigma * sigma)) * kInvSqrt2Pi / sigma; } std::vector Sharpen(const std::vector& image, int w, int h, float sigma, float amount) { // This is only made for small sigma, e.g. 1.3. std::vector kernel(5); for (size_t i = 0; i < kernel.size(); i++) { kernel[i] = Normal(1.0 * i - kernel.size() / 2, sigma); } double sum = 0; for (size_t i = 0; i < kernel.size(); i++) sum += kernel[i]; const double mul = 1.0 / sum; std::vector result = Convolve2X(image, w, h, kernel.data(), kernel.size(), mul); for (size_t i = 0; i < image.size(); i++) { result[i] = image[i] + (image[i] - result[i]) * amount; } return result; } void Erode(int w, int h, std::vector* image) { std::vector temp = *image; for (int y = 1; y + 1 < h; y++) { for (int x = 1; x + 1 < w; x++) { size_t index = y * w + x; if (!(temp[index] && temp[index - 1] && temp[index + 1] && temp[index - w] && temp[index + w])) { (*image)[index] = 0; } } } } void Dilate(int w, int h, std::vector* image) { std::vector temp = *image; for (int y = 1; y + 1 < h; y++) { for (int x = 1; x + 1 < w; x++) { size_t index = y * w + x; if (temp[index] || temp[index - 1] || temp[index + 1] || temp[index - w] || temp[index + w]) { (*image)[index] = 1; } } } } std::vector Blur(const std::vector& image, int w, int h) { // This is only made for small sigma, e.g. 1.3. static const double kSigma = 1.3; std::vector kernel(5); for (size_t i = 0; i < kernel.size(); i++) { kernel[i] = Normal(1.0 * i - kernel.size() / 2, kSigma); } double sum = 0; for (size_t i = 0; i < kernel.size(); i++) sum += kernel[i]; const double mul = 1.0 / sum; return Convolve2X(image, w, h, kernel.data(), kernel.size(), mul); } } // namespace namespace guetzli { // Do the sharpening to the v channel, but only in areas where it will help // channel should be 2 for v sharpening, or 1 for less effective u sharpening std::vector> PreProcessChannel( int w, int h, int channel, float sigma, float amount, bool blur, bool sharpen, const std::vector>& image) { if (!blur && !sharpen) return image; // Bring in range 0.0-1.0 for Y, -0.5 - 0.5 for U and V auto yuv = image; for (size_t i = 0; i < yuv[0].size(); i++) { yuv[0][i] /= 255.0; yuv[1][i] = yuv[1][i] / 255.0f - 0.5f; yuv[2][i] = yuv[2][i] / 255.0f - 0.5f; } // Map of areas where the image is not too bright to apply the effect. std::vector darkmap(image[0].size(), false); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { size_t index = y * w + x; float y = yuv[0][index]; float u = yuv[1][index]; float v = yuv[2][index]; float r = y + 1.402f * v; float g = y - 0.34414f * u - 0.71414f * v; float b = y + 1.772f * u; // Parameters tuned to avoid sharpening in too bright areas, where the // effect makes it worse instead of better. if (channel == 2 && g < 0.85 && b < 0.85 && r < 0.9) { darkmap[index] = true; } if (channel == 1 && r < 0.85 && g < 0.85 && b < 0.9) { darkmap[index] = true; } } } Erode(w, h, &darkmap); Erode(w, h, &darkmap); Erode(w, h, &darkmap); // Map of areas where the image is red enough (blue in case of u channel). std::vector redmap(image[0].size(), false); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { size_t index = y * w + x; float u = yuv[1][index]; float v = yuv[2][index]; // Parameters tuned to allow only colors on which sharpening is useful. if (channel == 2 && 2.116 * v > -0.34414 * u + 0.2 && 1.402 * v > 1.772 * u + 0.2) { redmap[index] = true; } if (channel == 1 && v < 1.263 * u - 0.1 && u > -0.33741 * v) { redmap[index] = true; } } } Dilate(w, h, &redmap); Dilate(w, h, &redmap); Dilate(w, h, &redmap); // Map of areas where to allow sharpening by combining red and dark areas std::vector sharpenmap(image[0].size(), 0); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { size_t index = y * w + x; sharpenmap[index] = redmap[index] && darkmap[index]; } } // Threshold for where considered an edge. const double threshold = (channel == 2 ? 0.02 : 1.0) * 127.5; static const double kEdgeMatrix[9] = { 0, -1, 0, -1, 4, -1, 0, -1, 0 }; // Map of areas where to allow blurring, only where it is not too sharp std::vector blurmap(image[0].size(), false); std::vector edge = Convolve2D(yuv[channel], w, h, kEdgeMatrix, 3); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { size_t index = y * w + x; float u = yuv[1][index]; float v = yuv[2][index]; if (sharpenmap[index]) continue; if (!darkmap[index]) continue; if (fabs(edge[index]) < threshold && v < -0.162 * u) { blurmap[index] = true; } } } Erode(w, h, &blurmap); Erode(w, h, &blurmap); // Choose sharpened, blurred or original per pixel std::vector sharpened = Sharpen(yuv[channel], w, h, sigma, amount); std::vector blurred = Blur(yuv[channel], w, h); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { size_t index = y * w + x; if (sharpenmap[index]) { if (sharpen) yuv[channel][index] = sharpened[index]; } else if (blurmap[index]) { if (blur) yuv[channel][index] = blurred[index]; } } } // Bring back to range 0-255 for (size_t i = 0; i < yuv[0].size(); i++) { yuv[0][i] *= 255.0; yuv[1][i] = (yuv[1][i] + 0.5f) * 255.0f; yuv[2][i] = (yuv[2][i] + 0.5f) * 255.0f; } return yuv; } namespace { inline float Clip(float val) { return std::max(0.0f, std::min(255.0f, val)); } inline float RGBToY(float r, float g, float b) { return 0.299f * r + 0.587f * g + 0.114f * b; } inline float RGBToU(float r, float g, float b) { return -0.16874f * r - 0.33126f * g + 0.5f * b + 128.0f; } inline float RGBToV(float r, float g, float b) { return 0.5f * r - 0.41869f * g - 0.08131f * b + 128.0f; } inline float YUVToR(float y, float u, float v) { return y + 1.402f * (v - 128.0f); } inline float YUVToG(float y, float u, float v) { return y - 0.344136f * (u - 128.0f) - 0.714136f * (v - 128.0f); } inline float YUVToB(float y, float u, float v) { return y + 1.772f * (u - 128.0f); } // TODO(user) Use SRGB->linear conversion and a lookup-table. inline float GammaToLinear(float x) { return static_cast(std::pow(x / 255.0f, 2.2)); } // TODO(user) Use linear->SRGB conversion and a lookup-table. inline float LinearToGamma(float x) { return 255.0 * std::pow(x, 1.0 / 2.2); } std::vector LinearlyAveragedLuma(const std::vector& rgb) { assert(rgb.size() % 3 == 0); std::vector y(rgb.size() / 3); for (size_t i = 0, p = 0; p < rgb.size(); ++i, p += 3) { y[i] = LinearToGamma(RGBToY(GammaToLinear(rgb[p + 0]), GammaToLinear(rgb[p + 1]), GammaToLinear(rgb[p + 2]))); } return y; } std::vector LinearlyDownsample2x2(const std::vector& rgb_in, const int width, const int height) { assert(rgb_in.size() == 3 * width * height); int w = (width + 1) / 2; int h = (height + 1) / 2; std::vector rgb_out(3 * w * h); for (int y = 0, p = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { for (int i = 0; i < 3; ++i, ++p) { rgb_out[p] = 0.0; for (int iy = 0; iy < 2; ++iy) { for (int ix = 0; ix < 2; ++ix) { int yy = std::min(height - 1, 2 * y + iy); int xx = std::min(width - 1, 2 * x + ix); rgb_out[p] += GammaToLinear(rgb_in[3 * (yy * width + xx) + i]); } } rgb_out[p] = LinearToGamma(0.25f * rgb_out[p]); } } } return rgb_out; } std::vector > RGBToYUV(const std::vector& rgb) { std::vector > yuv(3, std::vector(rgb.size() / 3)); for (size_t i = 0, p = 0; p < rgb.size(); ++i, p += 3) { const float r = rgb[p + 0]; const float g = rgb[p + 1]; const float b = rgb[p + 2]; yuv[0][i] = RGBToY(r, g, b); yuv[1][i] = RGBToU(r, g, b); yuv[2][i] = RGBToV(r, g, b); } return yuv; } std::vector YUVToRGB(const std::vector >& yuv) { std::vector rgb(3 * yuv[0].size()); for (size_t i = 0, p = 0; p < rgb.size(); ++i, p += 3) { const float y = yuv[0][i]; const float u = yuv[1][i]; const float v = yuv[2][i]; rgb[p + 0] = Clip(YUVToR(y, u, v)); rgb[p + 1] = Clip(YUVToG(y, u, v)); rgb[p + 2] = Clip(YUVToB(y, u, v)); } return rgb; } // Upsamples img_in with a box-filter, and returns an image with output // dimensions width x height. std::vector Upsample2x2(const std::vector& img_in, const int width, const int height) { int w = (width + 1) / 2; int h = (height + 1) / 2; assert(img_in.size() == w * h); std::vector img_out(width * height); for (int y = 0, p = 0; y < h; ++y) { for (int x = 0; x < w; ++x, ++p) { for (int iy = 0; iy < 2; ++iy) { for (int ix = 0; ix < 2; ++ix) { int yy = std::min(height - 1, 2 * y + iy); int xx = std::min(width - 1, 2 * x + ix); img_out[yy * width + xx] = img_in[p]; } } } } return img_out; } // Apply the "fancy upsample" filter used by libjpeg. std::vector Blur(const std::vector& img, const int width, const int height) { std::vector img_out(width * height); for (int y0 = 0; y0 < height; y0 += 2) { for (int x0 = 0; x0 < width; x0 += 2) { for (int iy = 0; iy < 2 && y0 + iy < height; ++iy) { for (int ix = 0; ix < 2 && x0 + ix < width; ++ix) { int dy = 4 * iy - 2; int dx = 4 * ix - 2; int x1 = std::min(width - 1, std::max(0, x0 + dx)); int y1 = std::min(height - 1, std::max(0, y0 + dy)); img_out[(y0 + iy) * width + x0 + ix] = (9.0f * img[y0 * width + x0] + 3.0f * img[y0 * width + x1] + 3.0f * img[y1 * width + x0] + 1.0f * img[y1 * width + x1]) / 16.0f; } } } } return img_out; } std::vector YUV420ToRGB(const std::vector >& yuv420, const int width, const int height) { std::vector > yuv; yuv.push_back(yuv420[0]); std::vector u = Upsample2x2(yuv420[1], width, height); std::vector v = Upsample2x2(yuv420[2], width, height); yuv.push_back(Blur(u, width, height)); yuv.push_back(Blur(v, width, height)); return YUVToRGB(yuv); } void UpdateGuess(const std::vector& target, const std::vector& reconstructed, std::vector* guess) { assert(reconstructed.size() == guess->size()); assert(target.size() == guess->size()); for (size_t i = 0; i < guess->size(); ++i) { // TODO(user): Evaluate using a decaying constant here. (*guess)[i] = Clip((*guess)[i] - (reconstructed[i] - target[i])); } } } // namespace std::vector > RGBToYUV420( const std::vector& rgb_in, const int width, const int height) { std::vector rgbf(rgb_in.size()); for (size_t i = 0; i < rgb_in.size(); ++i) { rgbf[i] = static_cast(rgb_in[i]); } std::vector y_target = LinearlyAveragedLuma(rgbf); std::vector > yuv_target = RGBToYUV(LinearlyDownsample2x2(rgbf, width, height)); std::vector > yuv_guess = yuv_target; yuv_guess[0] = Upsample2x2(yuv_guess[0], width, height); // TODO(user): Stop early if the error is small enough. for (int iter = 0; iter < 20; ++iter) { std::vector rgb_rec = YUV420ToRGB(yuv_guess, width, height); std::vector y_rec = LinearlyAveragedLuma(rgb_rec); std::vector > yuv_rec = RGBToYUV(LinearlyDownsample2x2(rgb_rec, width, height)); UpdateGuess(y_target, y_rec, &yuv_guess[0]); UpdateGuess(yuv_target[1], yuv_rec[1], &yuv_guess[1]); UpdateGuess(yuv_target[2], yuv_rec[2], &yuv_guess[2]); } yuv_guess[1] = Upsample2x2(yuv_guess[1], width, height); yuv_guess[2] = Upsample2x2(yuv_guess[2], width, height); return yuv_guess; } } // namespace guetzli ================================================ FILE: guetzli/preprocess_downsample.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // Preprocesses U and V channel for better results after downsampling. #ifndef GUETZLI_PREPROCESS_DOWNSAMPLE_H_ #define GUETZLI_PREPROCESS_DOWNSAMPLE_H_ #include #include namespace guetzli { // Preprocesses the u (1) or v (2) channel of the given YUV image (range 0-255). std::vector> PreProcessChannel( int w, int h, int channel, float sigma, float amount, bool blur, bool sharpen, const std::vector>& image); // Gamma-compensated chroma subsampling. // Returns Y, U, V image planes, each with width x height dimensions, but the // U and V planes are composed of 2x2 blocks with the same values. std::vector > RGBToYUV420( const std::vector& rgb_in, const int width, const int height); } // namespace guetzli #endif // GUETZLI_PREPROCESS_DOWNSAMPLE_H_ ================================================ FILE: guetzli/processor.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/processor.h" #include #include #include #include #include "guetzli/butteraugli_comparator.h" #include "guetzli/comparator.h" #include "guetzli/debug_print.h" #include "guetzli/fast_log.h" #include "guetzli/jpeg_data_decoder.h" #include "guetzli/jpeg_data_encoder.h" #include "guetzli/jpeg_data_reader.h" #include "guetzli/jpeg_data_writer.h" #include "guetzli/output_image.h" #include "guetzli/quantize.h" namespace guetzli { namespace { static const size_t kBlockSize = 3 * kDCTBlockSize; struct CoeffData { int idx; float block_err; }; struct QuantData { int q[3][kDCTBlockSize]; size_t jpg_size; bool dist_ok; }; class Processor { public: bool ProcessJpegData(const Params& params, const JPEGData& jpg_in, Comparator* comparator, GuetzliOutput* out, ProcessStats* stats); private: void SelectFrequencyMasking(const JPEGData& jpg, OutputImage* img, const uint8_t comp_mask, const double target_mul, bool stop_early); void ComputeBlockZeroingOrder( const coeff_t block[kBlockSize], const coeff_t orig_block[kBlockSize], const int block_x, const int block_y, const int factor_x, const int factor_y, const uint8_t comp_mask, OutputImage* img, std::vector* output_order); bool SelectQuantMatrix(const JPEGData& jpg_in, const bool downsample, int best_q[3][kDCTBlockSize], OutputImage* img); QuantData TryQuantMatrix(const JPEGData& jpg_in, const float target_mul, int q[3][kDCTBlockSize], OutputImage* img); void MaybeOutput(const std::string& encoded_jpg); void DownsampleImage(OutputImage* img); void OutputJpeg(const JPEGData& in, std::string* out); Params params_; Comparator* comparator_; GuetzliOutput* final_output_; ProcessStats* stats_; }; void RemoveOriginalQuantization(JPEGData* jpg, int q_in[3][kDCTBlockSize]) { for (int i = 0; i < 3; ++i) { JPEGComponent& c = jpg->components[i]; const int* q = &jpg->quant[c.quant_idx].values[0]; memcpy(&q_in[i][0], q, kDCTBlockSize * sizeof(q[0])); for (size_t j = 0; j < c.coeffs.size(); ++j) { c.coeffs[j] *= q[j % kDCTBlockSize]; } } int q[3][kDCTBlockSize]; for (int i = 0; i < 3; ++i) for (int j = 0; j < kDCTBlockSize; ++j) q[i][j] = 1; SaveQuantTables(q, jpg); } void Processor::DownsampleImage(OutputImage* img) { if (img->component(1).factor_x() > 1 || img->component(1).factor_y() > 1) { return; } OutputImage::DownsampleConfig cfg; cfg.use_silver_screen = params_.use_silver_screen; img->Downsample(cfg); } bool CheckJpegSanity(const JPEGData& jpg) { const int kMaxComponent = 1 << 12; for (const JPEGComponent& comp : jpg.components) { const JPEGQuantTable& quant_table = jpg.quant[comp.quant_idx]; for (int i = 0; i < comp.coeffs.size(); i++) { coeff_t coeff = comp.coeffs[i]; int quant = quant_table.values[i % kDCTBlockSize]; if (std::abs(static_cast(coeff) * quant) > kMaxComponent) { return false; } } } return true; } } // namespace int GuetzliStringOut(void* data, const uint8_t* buf, size_t count) { std::string* sink = reinterpret_cast(data); sink->append(reinterpret_cast(buf), count); return count; } void Processor::OutputJpeg(const JPEGData& jpg, std::string* out) { out->clear(); JPEGOutput output(GuetzliStringOut, out); if (!WriteJpeg(jpg, params_.clear_metadata, output)) { assert(0); } } void Processor::MaybeOutput(const std::string& encoded_jpg) { double score = comparator_->ScoreOutputSize(encoded_jpg.size()); GUETZLI_LOG(stats_, " Score[%.4f]", score); if (score < final_output_->score || final_output_->score < 0) { final_output_->jpeg_data = encoded_jpg; final_output_->score = score; GUETZLI_LOG(stats_, " (*)"); } GUETZLI_LOG(stats_, "\n"); } bool CompareQuantData(const QuantData& a, const QuantData& b) { if (a.dist_ok && !b.dist_ok) return true; if (!a.dist_ok && b.dist_ok) return false; return a.jpg_size < b.jpg_size; } // Compares a[0..kBlockSize) and b[0..kBlockSize) vectors, and returns // 0 : if they are equal // -1 : if a is everywhere <= than b and in at least one coordinate < // 1 : if a is everywhere >= than b and in at least one coordinate > // 2 : if a and b are uncomparable (some coordinate smaller and some greater) int CompareQuantMatrices(const int* a, const int* b) { int i = 0; while (i < kBlockSize && a[i] == b[i]) ++i; if (i == kBlockSize) { return 0; } if (a[i] < b[i]) { for (++i; i < kBlockSize; ++i) { if (a[i] > b[i]) return 2; } return -1; } else { for (++i; i < kBlockSize; ++i) { if (a[i] < b[i]) return 2; } return 1; } } double ContrastSensitivity(int k) { return 1.0 / (1.0 + kJPEGZigZagOrder[k] / 2.0); } double QuantMatrixHeuristicScore(const int q[3][kDCTBlockSize]) { double score = 0.0; for (int c = 0; c < 3; ++c) { for (int k = 0; k < kDCTBlockSize; ++k) { score += 0.5 * (q[c][k] - 1.0) * ContrastSensitivity(k); } } return score; } class QuantMatrixGenerator { public: QuantMatrixGenerator(bool downsample, ProcessStats* stats) : downsample_(downsample), hscore_a_(-1.0), hscore_b_(-1.0), total_csf_(0.0), stats_(stats) { for (int k = 0; k < kDCTBlockSize; ++k) { total_csf_ += 3.0 * ContrastSensitivity(k); } } bool GetNext(int q[3][kDCTBlockSize]) { // This loop should terminate by return. This 1000 iteration limit is just a // precaution. for (int iter = 0; iter < 1000; iter++) { double hscore; if (hscore_b_ == -1.0) { if (hscore_a_ == -1.0) { hscore = downsample_ ? 0.0 : total_csf_; } else { if (hscore_a_ < 5.0 * total_csf_) { hscore = hscore_a_ + total_csf_; } else { hscore = 2 * (hscore_a_ + total_csf_); } } if (hscore > 100 * total_csf_) { // We could not find a quantization matrix that creates enough // butteraugli error. This can happen if all dct coefficients are // close to zero in the original image. return false; } } else if (hscore_b_ == 0.0) { return false; } else if (hscore_a_ == -1.0) { hscore = 0.0; } else { int lower_q[3][kDCTBlockSize]; int upper_q[3][kDCTBlockSize]; constexpr double kEps = 0.05; GetQuantMatrixWithHeuristicScore( (1 - kEps) * hscore_a_ + kEps * 0.5 * (hscore_a_ + hscore_b_), lower_q); GetQuantMatrixWithHeuristicScore( (1 - kEps) * hscore_b_ + kEps * 0.5 * (hscore_a_ + hscore_b_), upper_q); if (CompareQuantMatrices(&lower_q[0][0], &upper_q[0][0]) == 0) return false; hscore = (hscore_a_ + hscore_b_) * 0.5; } GetQuantMatrixWithHeuristicScore(hscore, q); bool retry = false; for (size_t i = 0; i < quants_.size(); ++i) { if (CompareQuantMatrices(&q[0][0], &quants_[i].q[0][0]) == 0) { if (quants_[i].dist_ok) { hscore_a_ = hscore; } else { hscore_b_ = hscore; } retry = true; break; } } if (!retry) return true; } return false; } void Add(const QuantData& data) { quants_.push_back(data); double hscore = QuantMatrixHeuristicScore(data.q); if (data.dist_ok) { hscore_a_ = std::max(hscore_a_, hscore); } else { hscore_b_ = hscore_b_ == -1.0 ? hscore : std::min(hscore_b_, hscore); } } private: void GetQuantMatrixWithHeuristicScore(double score, int q[3][kDCTBlockSize]) const { int level = static_cast(score / total_csf_); score -= level * total_csf_; for (int k = kDCTBlockSize - 1; k >= 0; --k) { for (int c = 0; c < 3; ++c) { q[c][kJPEGNaturalOrder[k]] = 2 * level + (score > 0.0 ? 3 : 1); } score -= 3.0 * ContrastSensitivity(kJPEGNaturalOrder[k]); } } const bool downsample_; // Lower bound for quant matrix heuristic score used in binary search. double hscore_a_; // Upper bound for quant matrix heuristic score used in binary search, or 0.0 // if no upper bound is found yet. double hscore_b_; // Cached value of the sum of all ContrastSensitivity() values over all // quant matrix elements. double total_csf_; std::vector quants_; ProcessStats* stats_; }; QuantData Processor::TryQuantMatrix(const JPEGData& jpg_in, const float target_mul, int q[3][kDCTBlockSize], OutputImage* img) { QuantData data; memcpy(data.q, q, sizeof(data.q)); img->CopyFromJpegData(jpg_in); img->ApplyGlobalQuantization(data.q); std::string encoded_jpg; { JPEGData jpg_out = jpg_in; img->SaveToJpegData(&jpg_out); OutputJpeg(jpg_out, &encoded_jpg); } GUETZLI_LOG(stats_, "Iter %2d: %s quantization matrix:\n", stats_->counters[kNumItersCnt] + 1, img->FrameTypeStr().c_str()); GUETZLI_LOG_QUANT(stats_, q); GUETZLI_LOG(stats_, "Iter %2d: %s GQ[%5.2f] Out[%7zd]", stats_->counters[kNumItersCnt] + 1, img->FrameTypeStr().c_str(), QuantMatrixHeuristicScore(q), encoded_jpg.size()); ++stats_->counters[kNumItersCnt]; comparator_->Compare(*img); data.dist_ok = comparator_->DistanceOK(target_mul); data.jpg_size = encoded_jpg.size(); MaybeOutput(encoded_jpg); return data; } bool Processor::SelectQuantMatrix(const JPEGData& jpg_in, const bool downsample, int best_q[3][kDCTBlockSize], OutputImage* img) { QuantMatrixGenerator qgen(downsample, stats_); // Don't try to go up to exactly the target distance when selecting a // quantization matrix, since we will need some slack to do the frequency // masking later. const float target_mul_high = 0.97f; const float target_mul_low = 0.95f; QuantData best = TryQuantMatrix(jpg_in, target_mul_high, best_q, img); for (;;) { int q_next[3][kDCTBlockSize]; if (!qgen.GetNext(q_next)) { break; } QuantData data = TryQuantMatrix(jpg_in, target_mul_high, q_next, img); qgen.Add(data); if (CompareQuantData(data, best)) { best = data; if (data.dist_ok && !comparator_->DistanceOK(target_mul_low)) { break; } } } memcpy(&best_q[0][0], &best.q[0][0], kBlockSize * sizeof(best_q[0][0])); GUETZLI_LOG(stats_, "\n%s selected quantization matrix:\n", downsample ? "YUV420" : "YUV444"); GUETZLI_LOG_QUANT(stats_, best_q); return best.dist_ok; } // REQUIRES: block[c*64...(c*64+63)] is all zero if (comp_mask & (1<* output_order) { static const uint8_t oldCsf[kDCTBlockSize] = { 10, 10, 20, 40, 60, 70, 80, 90, 10, 20, 30, 60, 70, 80, 90, 90, 20, 30, 60, 70, 80, 90, 90, 90, 40, 60, 70, 80, 90, 90, 90, 90, 60, 70, 80, 90, 90, 90, 90, 90, 70, 80, 90, 90, 90, 90, 90, 90, 80, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, }; static const double kWeight[3] = { 1.0, 0.22, 0.20 }; #include "guetzli/order.inc" std::vector > input_order; for (int c = 0; c < 3; ++c) { if (!(comp_mask & (1 << c))) continue; for (int k = 1; k < kDCTBlockSize; ++k) { int idx = c * kDCTBlockSize + k; if (block[idx] != 0) { float score; if (params_.new_zeroing_model) { score = std::abs(orig_block[idx]) * csf[idx] + bias[idx]; } else { score = static_cast((std::abs(orig_block[idx]) - kJPEGZigZagOrder[k] / 64.0) * kWeight[c] / oldCsf[k]); } input_order.push_back(std::make_pair(idx, score)); } } } std::sort(input_order.begin(), input_order.end(), [](const std::pair& a, const std::pair& b) { return a.second < b.second; }); coeff_t processed_block[kBlockSize]; memcpy(processed_block, block, sizeof(processed_block)); comparator_->SwitchBlock(block_x, block_y, factor_x, factor_y); while (!input_order.empty()) { float best_err = 1e17f; int best_i = 0; for (size_t i = 0; i < std::min(params_.zeroing_greedy_lookahead, input_order.size()); ++i) { coeff_t candidate_block[kBlockSize]; memcpy(candidate_block, processed_block, sizeof(candidate_block)); const int idx = input_order[i].first; candidate_block[idx] = 0; for (int c = 0; c < 3; ++c) { if (comp_mask & (1 << c)) { img->component(c).SetCoeffBlock( block_x, block_y, &candidate_block[c * kDCTBlockSize]); } } float max_err = 0; for (int iy = 0; iy < factor_y; ++iy) { for (int ix = 0; ix < factor_x; ++ix) { int block_xx = block_x * factor_x + ix; int block_yy = block_y * factor_y + iy; if (8 * block_xx < img->width() && 8 * block_yy < img->height()) { float err = static_cast(comparator_->CompareBlock(*img, ix, iy)); max_err = std::max(max_err, err); } } } if (max_err < best_err) { best_err = max_err; best_i = i; } } int idx = input_order[best_i].first; processed_block[idx] = 0; input_order.erase(input_order.begin() + best_i); output_order->push_back({idx, best_err}); for (int c = 0; c < 3; ++c) { if (comp_mask & (1 << c)) { img->component(c).SetCoeffBlock( block_x, block_y, &processed_block[c * kDCTBlockSize]); } } } // Make the block error values monotonic. float min_err = 1e10; for (int i = output_order->size() - 1; i >= 0; --i) { min_err = std::min(min_err, (*output_order)[i].block_err); (*output_order)[i].block_err = min_err; } // Cut off at the block error limit. size_t num = 0; while (num < output_order->size() && (*output_order)[num].block_err <= comparator_->BlockErrorLimit()) { ++num; } output_order->resize(num); // Restore *img to the same state as it was at the start of this function. for (int c = 0; c < 3; ++c) { if (comp_mask & (1 << c)) { img->component(c).SetCoeffBlock( block_x, block_y, &block[c * kDCTBlockSize]); } } } namespace { void UpdateACHistogram(const int weight, const coeff_t* coeffs, const int* q, JpegHistogram* ac_histogram) { int r = 0; for (int k = 1; k < 64; ++k) { const int k_nat = kJPEGNaturalOrder[k]; coeff_t coeff = coeffs[k_nat]; if (coeff == 0) { r++; continue; } while (r > 15) { ac_histogram->Add(0xf0, weight); r -= 16; } int nbits = Log2FloorNonZero(std::abs(coeff / q[k_nat])) + 1; int symbol = (r << 4) + nbits; ac_histogram->Add(symbol, weight); r = 0; } if (r > 0) { ac_histogram->Add(0, weight); } } size_t ComputeEntropyCodes(const std::vector& histograms, std::vector* depths) { std::vector clustered = histograms; size_t num = histograms.size(); std::vector indexes(histograms.size()); std::vector clustered_depths( histograms.size() * JpegHistogram::kSize); ClusterHistograms(&clustered[0], &num, &indexes[0], &clustered_depths[0]); depths->resize(clustered_depths.size()); for (size_t i = 0; i < histograms.size(); ++i) { memcpy(&(*depths)[i * JpegHistogram::kSize], &clustered_depths[indexes[i] * JpegHistogram::kSize], JpegHistogram::kSize); } size_t histogram_size = 0; for (size_t i = 0; i < num; ++i) { histogram_size += HistogramHeaderCost(clustered[i]) / 8; } return histogram_size; } size_t EntropyCodedDataSize(const std::vector& histograms, const std::vector& depths) { size_t numbits = 0; for (size_t i = 0; i < histograms.size(); ++i) { numbits += HistogramEntropyCost( histograms[i], &depths[i * JpegHistogram::kSize]); } return (numbits + 7) / 8; } size_t EstimateDCSize(const JPEGData& jpg) { std::vector histograms(jpg.components.size()); BuildDCHistograms(jpg, &histograms[0]); size_t num = histograms.size(); std::vector indexes(num); std::vector depths(num * JpegHistogram::kSize); return ClusterHistograms(&histograms[0], &num, &indexes[0], &depths[0]); } } // namespace void Processor::SelectFrequencyMasking(const JPEGData& jpg, OutputImage* img, const uint8_t comp_mask, const double target_mul, bool stop_early) { const int width = img->width(); const int height = img->height(); const int ncomp = jpg.components.size(); const int last_c = Log2FloorNonZero(comp_mask); if (static_cast(last_c) >= jpg.components.size()) return; const int factor_x = img->component(last_c).factor_x(); const int factor_y = img->component(last_c).factor_y(); const int block_width = (width + 8 * factor_x - 1) / (8 * factor_x); const int block_height = (height + 8 * factor_y - 1) / (8 * factor_y); const int num_blocks = block_width * block_height; std::vector candidate_coeff_offsets(num_blocks + 1); std::vector candidate_coeffs; std::vector candidate_coeff_errors; candidate_coeffs.reserve(60 * num_blocks); candidate_coeff_errors.reserve(60 * num_blocks); std::vector block_order; block_order.reserve(3 * kDCTBlockSize); comparator_->StartBlockComparisons(); for (int block_y = 0, block_ix = 0; block_y < block_height; ++block_y) { for (int block_x = 0; block_x < block_width; ++block_x, ++block_ix) { coeff_t block[kBlockSize] = { 0 }; coeff_t orig_block[kBlockSize] = { 0 }; for (int c = 0; c < 3; ++c) { if (comp_mask & (1 << c)) { assert(img->component(c).factor_x() == factor_x); assert(img->component(c).factor_y() == factor_y); img->component(c).GetCoeffBlock(block_x, block_y, &block[c * kDCTBlockSize]); const JPEGComponent& comp = jpg.components[c]; int jpg_block_ix = block_y * comp.width_in_blocks + block_x; memcpy(&orig_block[c * kDCTBlockSize], &comp.coeffs[jpg_block_ix * kDCTBlockSize], kDCTBlockSize * sizeof(orig_block[0])); } } block_order.clear(); ComputeBlockZeroingOrder(block, orig_block, block_x, block_y, factor_x, factor_y, comp_mask, img, &block_order); candidate_coeff_offsets[block_ix] = candidate_coeffs.size(); for (size_t i = 0; i < block_order.size(); ++i) { candidate_coeffs.push_back(block_order[i].idx); candidate_coeff_errors.push_back(block_order[i].block_err); } } } comparator_->FinishBlockComparisons(); candidate_coeff_offsets[num_blocks] = candidate_coeffs.size(); std::vector ac_histograms(ncomp); int jpg_header_size, dc_size; { JPEGData jpg_out = jpg; img->SaveToJpegData(&jpg_out); jpg_header_size = JpegHeaderSize(jpg_out, params_.clear_metadata); dc_size = EstimateDCSize(jpg_out); BuildACHistograms(jpg_out, &ac_histograms[0]); } std::vector ac_depths; int ac_histogram_size = ComputeEntropyCodes(ac_histograms, &ac_depths); int base_size = jpg_header_size + dc_size + ac_histogram_size + EntropyCodedDataSize(ac_histograms, ac_depths); int prev_size = base_size; std::vector max_block_error(num_blocks); std::vector last_indexes(num_blocks); bool first_up_iter = true; for (int direction : {1, -1}) { for (;;) { if (stop_early && direction == -1) { if (prev_size > 1.01 * final_output_->jpeg_data.size()) { // If we are down-adjusting the error, the output size will only keep // increasing. // TODO(user): Do this check always by comparing only the size // of the currently processed components. break; } } std::vector > global_order; int blocks_to_change; std::vector block_weight; for (int rblock = 1; rblock <= 4; ++rblock) { block_weight = std::vector(num_blocks); std::vector distmap(width * height); if (!first_up_iter) { distmap = comparator_->distmap(); } comparator_->ComputeBlockErrorAdjustmentWeights( direction, rblock, target_mul, factor_x, factor_y, distmap, &block_weight); global_order.clear(); blocks_to_change = 0; for (int block_y = 0, block_ix = 0; block_y < block_height; ++block_y) { for (int block_x = 0; block_x < block_width; ++block_x, ++block_ix) { const int last_index = last_indexes[block_ix]; const int offset = candidate_coeff_offsets[block_ix]; const int num_candidates = candidate_coeff_offsets[block_ix + 1] - offset; const float* candidate_errors = &candidate_coeff_errors[offset]; const float max_err = max_block_error[block_ix]; if (block_weight[block_ix] == 0) { continue; } if (direction > 0) { for (size_t i = last_index; i < num_candidates; ++i) { float val = ((candidate_errors[i] - max_err) / block_weight[block_ix]); global_order.push_back(std::make_pair(block_ix, val)); } blocks_to_change += (last_index < num_candidates ? 1 : 0); } else { for (int i = last_index - 1; i >= 0; --i) { float val = ((max_err - candidate_errors[i]) / block_weight[block_ix]); global_order.push_back(std::make_pair(block_ix, val)); } blocks_to_change += (last_index > 0 ? 1 : 0); } } } if (!global_order.empty()) { // If we found something to adjust with the current block adjustment // radius, we can stop and adjust the blocks we have. break; } } if (global_order.empty()) { break; } std::sort(global_order.begin(), global_order.end(), [](const std::pair& a, const std::pair& b) { return a.second < b.second; }); double rel_size_delta = direction > 0 ? 0.01 : 0.0005; if (direction > 0 && comparator_->DistanceOK(1.0)) { rel_size_delta = 0.05; } double min_size_delta = base_size * rel_size_delta; float coeffs_to_change_per_block = direction > 0 ? 2.0f : factor_x * factor_y * 0.2f; int min_coeffs_to_change = coeffs_to_change_per_block * blocks_to_change; if (first_up_iter) { const float limit = 0.75f * comparator_->BlockErrorLimit(); auto it = std::partition_point(global_order.begin(), global_order.end(), [=](const std::pair& a) { return a.second < limit; }); min_coeffs_to_change = std::max(min_coeffs_to_change, it - global_order.begin()); first_up_iter = false; } std::set changed_blocks; float val_threshold = 0.0; int changed_coeffs = 0; int est_jpg_size = prev_size; for (size_t i = 0; i < global_order.size(); ++i) { const int block_ix = global_order[i].first; const int block_x = block_ix % block_width; const int block_y = block_ix / block_width; const int last_idx = last_indexes[block_ix]; const int offset = candidate_coeff_offsets[block_ix]; const uint8_t* candidates = &candidate_coeffs[offset]; const int idx = candidates[last_idx + std::min(direction, 0)]; const int c = idx / kDCTBlockSize; const int k = idx % kDCTBlockSize; const int* quant = img->component(c).quant(); const JPEGComponent& comp = jpg.components[c]; const int jpg_block_ix = block_y * comp.width_in_blocks + block_x; const int newval = direction > 0 ? 0 : Quantize( comp.coeffs[jpg_block_ix * kDCTBlockSize + k], quant[k]); coeff_t block[kDCTBlockSize] = { 0 }; img->component(c).GetCoeffBlock(block_x, block_y, block); UpdateACHistogram(-1, block, quant, &ac_histograms[c]); double sum_of_hf = 0; for (int ii = 3; ii < 64; ++ii) { if ((ii & 7) < 3 && ii < 3 * 8) continue; sum_of_hf += std::abs(comp.coeffs[jpg_block_ix * kDCTBlockSize + ii]); } int limit = sum_of_hf < 60 ? 4 : 8; bool precious = (k == 1 || k == 8) && std::abs(comp.coeffs[jpg_block_ix * kDCTBlockSize + k]) >= limit; if (!precious || newval != 0) { block[k] = newval; } UpdateACHistogram(1, block, quant, &ac_histograms[c]); img->component(c).SetCoeffBlock(block_x, block_y, block); last_indexes[block_ix] += direction; changed_blocks.insert(block_ix); val_threshold = global_order[i].second; ++changed_coeffs; static const int kEntropyCodeUpdateFreq = 10; if (i % kEntropyCodeUpdateFreq == 0) { ac_histogram_size = ComputeEntropyCodes(ac_histograms, &ac_depths); } est_jpg_size = jpg_header_size + dc_size + ac_histogram_size + EntropyCodedDataSize(ac_histograms, ac_depths); if (changed_coeffs > min_coeffs_to_change && std::abs(est_jpg_size - prev_size) > min_size_delta) { break; } } size_t global_order_size = global_order.size(); std::vector>().swap(global_order); for (int i = 0; i < num_blocks; ++i) { max_block_error[i] += block_weight[i] * val_threshold * direction; } ++stats_->counters[kNumItersCnt]; ++stats_->counters[direction > 0 ? kNumItersUpCnt : kNumItersDownCnt]; std::string encoded_jpg; { JPEGData jpg_out = jpg; img->SaveToJpegData(&jpg_out); OutputJpeg(jpg_out, &encoded_jpg); } GUETZLI_LOG(stats_, "Iter %2d: %s(%d) %s Coeffs[%d/%zd] " "Blocks[%zd/%d/%d] ValThres[%.4f] Out[%7zd] EstErr[%.2f%%]", stats_->counters[kNumItersCnt], img->FrameTypeStr().c_str(), comp_mask, direction > 0 ? "up" : "down", changed_coeffs, global_order_size, changed_blocks.size(), blocks_to_change, num_blocks, val_threshold, encoded_jpg.size(), 100.0 - (100.0 * est_jpg_size) / encoded_jpg.size()); comparator_->Compare(*img); MaybeOutput(encoded_jpg); prev_size = est_jpg_size; } } } bool IsGrayscale(const JPEGData& jpg) { for (int c = 1; c < 3; ++c) { const JPEGComponent& comp = jpg.components[c]; for (size_t i = 0; i < comp.coeffs.size(); ++i) { if (comp.coeffs[i] != 0) return false; } } return true; } bool Processor::ProcessJpegData(const Params& params, const JPEGData& jpg_in, Comparator* comparator, GuetzliOutput* out, ProcessStats* stats) { params_ = params; comparator_ = comparator; final_output_ = out; stats_ = stats; if (params.butteraugli_target > 2.0f) { fprintf(stderr, "Guetzli should be called with quality >= 84, otherwise the\n" "output will have noticeable artifacts. If you want to\n" "proceed anyway, please edit the source code.\n"); return false; } if (jpg_in.components.size() != 3 || !HasYCbCrColorSpace(jpg_in)) { fprintf(stderr, "Only YUV color space input jpeg is supported\n"); return false; } bool input_is_420; if (jpg_in.Is444()) { input_is_420 = false; } else if (jpg_in.Is420()) { input_is_420 = true; } else { fprintf(stderr, "Unsupported sampling factors:"); for (size_t i = 0; i < jpg_in.components.size(); ++i) { fprintf(stderr, " %dx%d", jpg_in.components[i].h_samp_factor, jpg_in.components[i].v_samp_factor); } fprintf(stderr, "\n"); return false; } int q_in[3][kDCTBlockSize]; // Output the original image, in case we do not manage to create anything // with a good enough quality. std::string encoded_jpg; OutputJpeg(jpg_in, &encoded_jpg); final_output_->score = -1; GUETZLI_LOG(stats, "Original Out[%7zd]", encoded_jpg.size()); if (comparator_ == nullptr) { GUETZLI_LOG(stats, " \n"); final_output_->jpeg_data = encoded_jpg; final_output_->score = encoded_jpg.size(); // Butteraugli doesn't work with images this small. return true; } { JPEGData jpg = jpg_in; RemoveOriginalQuantization(&jpg, q_in); OutputImage img(jpg.width, jpg.height); img.CopyFromJpegData(jpg); comparator_->Compare(img); } MaybeOutput(encoded_jpg); int try_420 = (input_is_420 || params_.force_420 || (params_.try_420 && !IsGrayscale(jpg_in))) ? 1 : 0; int force_420 = (input_is_420 || params_.force_420) ? 1 : 0; for (int downsample = force_420; downsample <= try_420; ++downsample) { JPEGData jpg = jpg_in; RemoveOriginalQuantization(&jpg, q_in); OutputImage img(jpg.width, jpg.height); img.CopyFromJpegData(jpg); if (downsample) { DownsampleImage(&img); img.SaveToJpegData(&jpg); } int best_q[3][kDCTBlockSize]; memcpy(best_q, q_in, sizeof(best_q)); if (!SelectQuantMatrix(jpg, downsample != 0, best_q, &img)) { for (int c = 0; c < 3; ++c) { for (int i = 0; i < kDCTBlockSize; ++i) { best_q[c][i] = 1; } } } img.CopyFromJpegData(jpg); img.ApplyGlobalQuantization(best_q); if (!downsample) { SelectFrequencyMasking(jpg, &img, 7, 1.0, false); } else { const float ymul = jpg.components.size() == 1 ? 1.0f : 0.97f; SelectFrequencyMasking(jpg, &img, 1, ymul, false); SelectFrequencyMasking(jpg, &img, 6, 1.0, true); } } return true; } bool ProcessJpegData(const Params& params, const JPEGData& jpg_in, Comparator* comparator, GuetzliOutput* out, ProcessStats* stats) { Processor processor; return processor.ProcessJpegData(params, jpg_in, comparator, out, stats); } bool Process(const Params& params, ProcessStats* stats, const std::string& data, std::string* jpg_out) { JPEGData jpg; if (!ReadJpeg(data, JPEG_READ_ALL, &jpg)) { fprintf(stderr, "Can't read jpg data from input file\n"); return false; } if (!CheckJpegSanity(jpg)) { fprintf(stderr, "Unsupported input JPEG (unexpectedly large coefficient " "values).\n"); return false; } std::vector rgb = DecodeJpegToRGB(jpg); if (rgb.empty()) { fprintf(stderr, "Unsupported input JPEG file (e.g. unsupported " "downsampling mode).\nPlease provide the input image as " "a PNG file.\n"); return false; } GuetzliOutput out; ProcessStats dummy_stats; if (stats == nullptr) { stats = &dummy_stats; } std::unique_ptr comparator; if (jpg.width >= 32 && jpg.height >= 32) { comparator.reset( new ButteraugliComparator(jpg.width, jpg.height, &rgb, params.butteraugli_target, stats)); } bool ok = ProcessJpegData(params, jpg, comparator.get(), &out, stats); *jpg_out = out.jpeg_data; return ok; } bool Process(const Params& params, ProcessStats* stats, const std::vector& rgb, int w, int h, std::string* jpg_out) { JPEGData jpg; if (!EncodeRGBToJpeg(rgb, w, h, &jpg)) { fprintf(stderr, "Could not create jpg data from rgb pixels\n"); return false; } GuetzliOutput out; ProcessStats dummy_stats; if (stats == nullptr) { stats = &dummy_stats; } std::unique_ptr comparator; if (jpg.width >= 32 && jpg.height >= 32) { comparator.reset( new ButteraugliComparator(jpg.width, jpg.height, &rgb, params.butteraugli_target, stats)); } bool ok = ProcessJpegData(params, jpg, comparator.get(), &out, stats); *jpg_out = out.jpeg_data; return ok; } } // namespace guetzli ================================================ FILE: guetzli/processor.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_PROCESSOR_H_ #define GUETZLI_PROCESSOR_H_ #include #include #include "guetzli/comparator.h" #include "guetzli/jpeg_data.h" #include "guetzli/stats.h" namespace guetzli { struct Params { float butteraugli_target = 1.0; bool clear_metadata = true; bool try_420 = false; bool force_420 = false; bool use_silver_screen = false; int zeroing_greedy_lookahead = 3; bool new_zeroing_model = true; }; bool Process(const Params& params, ProcessStats* stats, const std::string& in_data, std::string* out_data); struct GuetzliOutput { std::string jpeg_data; double score; }; bool ProcessJpegData(const Params& params, const JPEGData& jpg_in, Comparator* comparator, GuetzliOutput* out, ProcessStats* stats); // Sets *out to a jpeg encoded string that will decode to an image that is // visually indistinguishable from the input rgb image. bool Process(const Params& params, ProcessStats* stats, const std::vector& rgb, int w, int h, std::string* out); } // namespace guetzli #endif // GUETZLI_PROCESSOR_H_ ================================================ FILE: guetzli/quality.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/quality.h" namespace guetzli { namespace { constexpr int kLowestQuality = 70; constexpr int kHighestQuality = 110; // Butteraugli scores that correspond to JPEG quality levels, starting at // kLowestQuality. They were computed by taking median BA scores of JPEGs // generated using libjpeg-turbo at given quality from a set of PNGs. // The scores above quality level 100 are just linearly decreased so that score // for 110 is 90% of the score for 100. const double kScoreForQuality[] = { 2.810761, // 70 2.729300, 2.689687, 2.636811, 2.547863, 2.525400, 2.473416, 2.366133, 2.338078, 2.318654, 2.201674, // 80 2.145517, 2.087322, 2.009328, 1.945456, 1.900112, 1.805701, 1.750194, 1.644175, 1.562165, 1.473608, // 90 1.382021, 1.294298, 1.185402, 1.066781, 0.971769, // 95 0.852901, 0.724544, 0.611302, 0.443185, 0.211578, // 100 0.209462, 0.207346, 0.205230, 0.203114, 0.200999, // 105 0.198883, 0.196767, 0.194651, 0.192535, 0.190420, // 110 0.190420, }; } // namespace double ButteraugliScoreForQuality(double quality) { if (quality < kLowestQuality) quality = kLowestQuality; if (quality > kHighestQuality) quality = kHighestQuality; int index = static_cast(quality); double mix = quality - index; return kScoreForQuality[index - kLowestQuality] * (1 - mix) + kScoreForQuality[index - kLowestQuality + 1] * mix; } } // namespace guetzli ================================================ FILE: guetzli/quality.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_QUALITY_H_ #define GUETZLI_QUALITY_H_ namespace guetzli { double ButteraugliScoreForQuality(double quality); } // namespace guetzli #endif // GUETZLI_QUALITY_H_ ================================================ FILE: guetzli/quantize.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/quantize.h" namespace guetzli { bool QuantizeBlock(coeff_t block[kDCTBlockSize], const int q[kDCTBlockSize]) { bool changed = false; for (int k = 0; k < kDCTBlockSize; ++k) { coeff_t coeff = Quantize(block[k], q[k]); changed = changed || (coeff != block[k]); block[k] = coeff; } return changed; } } // namespace guetzli ================================================ FILE: guetzli/quantize.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_QUANTIZE_H_ #define GUETZLI_QUANTIZE_H_ #include "guetzli/jpeg_data.h" namespace guetzli { inline coeff_t Quantize(coeff_t raw_coeff, int quant) { const int r = raw_coeff % quant; const coeff_t delta = 2 * r > quant ? quant - r : (-2) * r > quant ? -quant - r : -r; return raw_coeff + delta; } bool QuantizeBlock(coeff_t block[kDCTBlockSize], const int q[kDCTBlockSize]); } // namespace guetzli #endif // GUETZLI_QUANTIZE_H_ ================================================ FILE: guetzli/score.cc ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "guetzli/score.h" #include namespace guetzli { double ScoreJPEG(double butteraugli_distance, int size, double butteraugli_target) { constexpr double kScale = 50; constexpr double kMaxExponent = 10; constexpr double kLargeSize = 1e30; // TODO(user): The score should also depend on distance below target (and be // smooth). double diff = butteraugli_distance - butteraugli_target; if (diff <= 0.0) { return size; } else { double exponent = kScale * diff; if (exponent > kMaxExponent) { return kLargeSize * std::exp(kMaxExponent) * diff + size; } else { return std::exp(exponent) * size; } } } } // namespace guetzli ================================================ FILE: guetzli/score.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_SCORE_H_ #define GUETZLI_SCORE_H_ #include namespace guetzli { double ScoreJPEG(double butteraugli_distance, int size, double butteraugli_target); } // namespace guetzli #endif // GUETZLI_SCORE_H_ ================================================ FILE: guetzli/stats.h ================================================ /* * Copyright 2016 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef GUETZLI_STATS_H_ #define GUETZLI_STATS_H_ #include #include #include #include #include namespace guetzli { static const char* const kNumItersCnt = "number of iterations"; static const char* const kNumItersUpCnt = "number of iterations up"; static const char* const kNumItersDownCnt = "number of iterations down"; struct ProcessStats { ProcessStats() {} std::map counters; std::string* debug_output = nullptr; FILE* debug_output_file = nullptr; std::string filename; }; } // namespace guetzli #endif // GUETZLI_STATS_H_ ================================================ FILE: guetzli.make ================================================ # GNU Make project makefile autogenerated by Premake ifndef config config=release endif ifndef verbose SILENT = @ endif .PHONY: clean prebuild prelink ifeq ($(config),release) RESCOMP = windres TARGETDIR = bin/Release TARGET = $(TARGETDIR)/guetzli OBJDIR = obj/Release/guetzli DEFINES += INCLUDES += -I. -Ithird_party/butteraugli FORCE_INCLUDE += ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES) ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -O3 -g `pkg-config --cflags libpng || libpng-config --cflags` ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -O3 -g -std=c++11 `pkg-config --cflags libpng || libpng-config --cflags` ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES) LIBS += LDDEPS += ALL_LDFLAGS += $(LDFLAGS) `pkg-config --libs libpng || libpng-config --ldflags` LINKCMD = $(CXX) -o "$@" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS) define PREBUILDCMDS endef define PRELINKCMDS endef define POSTBUILDCMDS endef all: $(TARGETDIR) $(OBJDIR) prebuild prelink $(TARGET) @: endif ifeq ($(config),debug) RESCOMP = windres TARGETDIR = bin/Debug TARGET = $(TARGETDIR)/guetzli OBJDIR = obj/Debug/guetzli DEFINES += INCLUDES += -I. -Ithird_party/butteraugli FORCE_INCLUDE += ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES) ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -g `pkg-config --cflags libpng || libpng-config --cflags` ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -g -std=c++11 `pkg-config --cflags libpng || libpng-config --cflags` ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES) LIBS += LDDEPS += ALL_LDFLAGS += $(LDFLAGS) `pkg-config --libs libpng || libpng-config --ldflags` LINKCMD = $(CXX) -o "$@" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS) define PREBUILDCMDS endef define PRELINKCMDS endef define POSTBUILDCMDS endef all: $(TARGETDIR) $(OBJDIR) prebuild prelink $(TARGET) @: endif OBJECTS := \ $(OBJDIR)/butteraugli_comparator.o \ $(OBJDIR)/dct_double.o \ $(OBJDIR)/debug_print.o \ $(OBJDIR)/entropy_encode.o \ $(OBJDIR)/fdct.o \ $(OBJDIR)/gamma_correct.o \ $(OBJDIR)/guetzli.o \ $(OBJDIR)/idct.o \ $(OBJDIR)/jpeg_data.o \ $(OBJDIR)/jpeg_data_decoder.o \ $(OBJDIR)/jpeg_data_encoder.o \ $(OBJDIR)/jpeg_data_reader.o \ $(OBJDIR)/jpeg_data_writer.o \ $(OBJDIR)/jpeg_huffman_decode.o \ $(OBJDIR)/output_image.o \ $(OBJDIR)/preprocess_downsample.o \ $(OBJDIR)/processor.o \ $(OBJDIR)/quality.o \ $(OBJDIR)/quantize.o \ $(OBJDIR)/score.o \ $(OBJDIR)/butteraugli.o \ RESOURCES := \ CUSTOMFILES := \ SHELLTYPE := msdos ifeq (,$(ComSpec)$(COMSPEC)) SHELLTYPE := posix endif ifeq (/bin,$(findstring /bin,$(SHELL))) SHELLTYPE := posix endif $(TARGET): $(GCH) ${CUSTOMFILES} $(OBJECTS) $(LDDEPS) $(RESOURCES) @echo Linking guetzli $(SILENT) $(LINKCMD) $(POSTBUILDCMDS) $(TARGETDIR): @echo Creating $(TARGETDIR) ifeq (posix,$(SHELLTYPE)) $(SILENT) mkdir -p $(TARGETDIR) else $(SILENT) mkdir $(subst /,\\,$(TARGETDIR)) endif $(OBJDIR): @echo Creating $(OBJDIR) ifeq (posix,$(SHELLTYPE)) $(SILENT) mkdir -p $(OBJDIR) else $(SILENT) mkdir $(subst /,\\,$(OBJDIR)) endif clean: @echo Cleaning guetzli ifeq (posix,$(SHELLTYPE)) $(SILENT) rm -f $(TARGET) $(SILENT) rm -rf $(OBJDIR) else $(SILENT) if exist $(subst /,\\,$(TARGET)) del $(subst /,\\,$(TARGET)) $(SILENT) if exist $(subst /,\\,$(OBJDIR)) rmdir /s /q $(subst /,\\,$(OBJDIR)) endif prebuild: $(PREBUILDCMDS) prelink: $(PRELINKCMDS) ifneq (,$(PCH)) $(OBJECTS): $(GCH) $(PCH) $(GCH): $(PCH) @echo $(notdir $<) $(SILENT) $(CXX) -x c++-header $(ALL_CXXFLAGS) -o "$@" -MF "$(@:%.gch=%.d)" -c "$<" endif $(OBJDIR)/butteraugli_comparator.o: guetzli/butteraugli_comparator.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/dct_double.o: guetzli/dct_double.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/debug_print.o: guetzli/debug_print.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/entropy_encode.o: guetzli/entropy_encode.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/fdct.o: guetzli/fdct.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/gamma_correct.o: guetzli/gamma_correct.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/guetzli.o: guetzli/guetzli.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/idct.o: guetzli/idct.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_data.o: guetzli/jpeg_data.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_data_decoder.o: guetzli/jpeg_data_decoder.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_data_encoder.o: guetzli/jpeg_data_encoder.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_data_reader.o: guetzli/jpeg_data_reader.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_data_writer.o: guetzli/jpeg_data_writer.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_huffman_decode.o: guetzli/jpeg_huffman_decode.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/output_image.o: guetzli/output_image.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/preprocess_downsample.o: guetzli/preprocess_downsample.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/processor.o: guetzli/processor.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/quality.o: guetzli/quality.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/quantize.o: guetzli/quantize.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/score.o: guetzli/score.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/butteraugli.o: third_party/butteraugli/butteraugli/butteraugli.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" -include $(OBJECTS:%.o=%.d) ifneq (,$(PCH)) -include $(OBJDIR)/$(notdir $(PCH)).d endif ================================================ FILE: guetzli.sln ================================================  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "guetzli_static", "guetzli_static.vcxproj", "{30B3C385-1C81-B78B-0515-28B2F18193F0}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "guetzli", "guetzli.vcxproj", "{C9FCBE14-35DC-3DB0-3EF4-C886AA52A411}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 Debug|x64 = Debug|x64 Release|Win32 = Release|Win32 Release|x64 = Release|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {30B3C385-1C81-B78B-0515-28B2F18193F0}.Debug|Win32.ActiveCfg = Debug|Win32 {30B3C385-1C81-B78B-0515-28B2F18193F0}.Debug|Win32.Build.0 = Debug|Win32 {30B3C385-1C81-B78B-0515-28B2F18193F0}.Debug|x64.ActiveCfg = Debug|x64 {30B3C385-1C81-B78B-0515-28B2F18193F0}.Debug|x64.Build.0 = Debug|x64 {30B3C385-1C81-B78B-0515-28B2F18193F0}.Release|Win32.ActiveCfg = Release|Win32 {30B3C385-1C81-B78B-0515-28B2F18193F0}.Release|Win32.Build.0 = Release|Win32 {30B3C385-1C81-B78B-0515-28B2F18193F0}.Release|x64.ActiveCfg = Release|x64 {30B3C385-1C81-B78B-0515-28B2F18193F0}.Release|x64.Build.0 = Release|x64 {C9FCBE14-35DC-3DB0-3EF4-C886AA52A411}.Debug|Win32.ActiveCfg = Debug|Win32 {C9FCBE14-35DC-3DB0-3EF4-C886AA52A411}.Debug|Win32.Build.0 = Debug|Win32 {C9FCBE14-35DC-3DB0-3EF4-C886AA52A411}.Debug|x64.ActiveCfg = Debug|x64 {C9FCBE14-35DC-3DB0-3EF4-C886AA52A411}.Debug|x64.Build.0 = Debug|x64 {C9FCBE14-35DC-3DB0-3EF4-C886AA52A411}.Release|Win32.ActiveCfg = Release|Win32 {C9FCBE14-35DC-3DB0-3EF4-C886AA52A411}.Release|Win32.Build.0 = Release|Win32 {C9FCBE14-35DC-3DB0-3EF4-C886AA52A411}.Release|x64.ActiveCfg = Release|x64 {C9FCBE14-35DC-3DB0-3EF4-C886AA52A411}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection EndGlobal ================================================ FILE: guetzli.vcxproj ================================================  Release x64 Release Win32 Debug x64 Debug Win32 {C9FCBE14-35DC-3DB0-3EF4-C886AA52A411} true Win32Proj guetzli Application false Unicode v140 Application false Unicode v140 Application true Unicode v140 Application true Unicode v140 false bin\x86_64\Release\ obj\x86_64\Release\guetzli\ guetzli .exe false bin\x86\Release\ obj\x86\Release\guetzli\ guetzli .exe true bin\x86_64\Debug\ obj\x86_64\Debug\guetzli\ guetzli .exe true bin\x86\Debug\ obj\x86\Debug\guetzli\ guetzli .exe NotUsing Level3 .;third_party\butteraugli;%(AdditionalIncludeDirectories) Full true true false true Console true true shlwapi.lib;%(AdditionalDependencies) mainCRTStartup NotUsing Level3 .;third_party\butteraugli;%(AdditionalIncludeDirectories) Full true true false true Console true true shlwapi.lib;%(AdditionalDependencies) mainCRTStartup NotUsing Level3 .;third_party\butteraugli;%(AdditionalIncludeDirectories) EditAndContinue Disabled Console true shlwapi.lib;%(AdditionalDependencies) mainCRTStartup NotUsing Level3 .;third_party\butteraugli;%(AdditionalIncludeDirectories) EditAndContinue Disabled Console true shlwapi.lib;%(AdditionalDependencies) mainCRTStartup ================================================ FILE: guetzli.vcxproj.filters ================================================ {C9FCBE14-35DC-3DB0-3EF4-C886AA52A411} {0FB18DF1-7B66-06E7-045B-00BE700FFDEA} {468B2B32-B2C2-73C9-BBCC-D7EC27839AC2} {FD6FCB41-6929-36EC-F288-50C65E41EC5B} guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli third_party\butteraugli\butteraugli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli third_party\butteraugli\butteraugli ================================================ FILE: guetzli_static.make ================================================ # GNU Make project makefile autogenerated by Premake ifndef config config=release endif ifndef verbose SILENT = @ endif .PHONY: clean prebuild prelink ifeq ($(config),release) RESCOMP = windres TARGETDIR = bin/Release TARGET = $(TARGETDIR)/libguetzli_static.a OBJDIR = obj/Release/guetzli_static DEFINES += INCLUDES += -I. -Ithird_party/butteraugli FORCE_INCLUDE += ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES) ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -O3 -g `pkg-config --static --cflags libpng || libpng-config --static --cflags` ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -O3 -g -std=c++11 `pkg-config --static --cflags libpng || libpng-config --static --cflags` ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES) LIBS += LDDEPS += ALL_LDFLAGS += $(LDFLAGS) `pkg-config --static --libs libpng || libpng-config --static --ldflags` LINKCMD = $(AR) -rcs "$@" $(OBJECTS) define PREBUILDCMDS endef define PRELINKCMDS endef define POSTBUILDCMDS endef all: $(TARGETDIR) $(OBJDIR) prebuild prelink $(TARGET) @: endif ifeq ($(config),debug) RESCOMP = windres TARGETDIR = bin/Debug TARGET = $(TARGETDIR)/libguetzli_static.a OBJDIR = obj/Debug/guetzli_static DEFINES += INCLUDES += -I. -Ithird_party/butteraugli FORCE_INCLUDE += ALL_CPPFLAGS += $(CPPFLAGS) -MMD -MP $(DEFINES) $(INCLUDES) ALL_CFLAGS += $(CFLAGS) $(ALL_CPPFLAGS) -g `pkg-config --static --cflags libpng || libpng-config --static --cflags` ALL_CXXFLAGS += $(CXXFLAGS) $(ALL_CPPFLAGS) -g -std=c++11 `pkg-config --static --cflags libpng || libpng-config --static --cflags` ALL_RESFLAGS += $(RESFLAGS) $(DEFINES) $(INCLUDES) LIBS += LDDEPS += ALL_LDFLAGS += $(LDFLAGS) `pkg-config --static --libs libpng || libpng-config --static --ldflags` LINKCMD = $(AR) -rcs "$@" $(OBJECTS) define PREBUILDCMDS endef define PRELINKCMDS endef define POSTBUILDCMDS endef all: $(TARGETDIR) $(OBJDIR) prebuild prelink $(TARGET) @: endif OBJECTS := \ $(OBJDIR)/butteraugli_comparator.o \ $(OBJDIR)/dct_double.o \ $(OBJDIR)/debug_print.o \ $(OBJDIR)/entropy_encode.o \ $(OBJDIR)/fdct.o \ $(OBJDIR)/gamma_correct.o \ $(OBJDIR)/idct.o \ $(OBJDIR)/jpeg_data.o \ $(OBJDIR)/jpeg_data_decoder.o \ $(OBJDIR)/jpeg_data_encoder.o \ $(OBJDIR)/jpeg_data_reader.o \ $(OBJDIR)/jpeg_data_writer.o \ $(OBJDIR)/jpeg_huffman_decode.o \ $(OBJDIR)/output_image.o \ $(OBJDIR)/preprocess_downsample.o \ $(OBJDIR)/processor.o \ $(OBJDIR)/quality.o \ $(OBJDIR)/quantize.o \ $(OBJDIR)/score.o \ $(OBJDIR)/butteraugli.o \ RESOURCES := \ CUSTOMFILES := \ SHELLTYPE := msdos ifeq (,$(ComSpec)$(COMSPEC)) SHELLTYPE := posix endif ifeq (/bin,$(findstring /bin,$(SHELL))) SHELLTYPE := posix endif $(TARGET): $(GCH) ${CUSTOMFILES} $(OBJECTS) $(LDDEPS) $(RESOURCES) @echo Linking guetzli_static $(SILENT) $(LINKCMD) $(POSTBUILDCMDS) $(TARGETDIR): @echo Creating $(TARGETDIR) ifeq (posix,$(SHELLTYPE)) $(SILENT) mkdir -p $(TARGETDIR) else $(SILENT) mkdir $(subst /,\\,$(TARGETDIR)) endif $(OBJDIR): @echo Creating $(OBJDIR) ifeq (posix,$(SHELLTYPE)) $(SILENT) mkdir -p $(OBJDIR) else $(SILENT) mkdir $(subst /,\\,$(OBJDIR)) endif clean: @echo Cleaning guetzli_static ifeq (posix,$(SHELLTYPE)) $(SILENT) rm -f $(TARGET) $(SILENT) rm -rf $(OBJDIR) else $(SILENT) if exist $(subst /,\\,$(TARGET)) del $(subst /,\\,$(TARGET)) $(SILENT) if exist $(subst /,\\,$(OBJDIR)) rmdir /s /q $(subst /,\\,$(OBJDIR)) endif prebuild: $(PREBUILDCMDS) prelink: $(PRELINKCMDS) ifneq (,$(PCH)) $(OBJECTS): $(GCH) $(PCH) $(GCH): $(PCH) @echo $(notdir $<) $(SILENT) $(CXX) -x c++-header $(ALL_CXXFLAGS) -o "$@" -MF "$(@:%.gch=%.d)" -c "$<" endif $(OBJDIR)/butteraugli_comparator.o: guetzli/butteraugli_comparator.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/dct_double.o: guetzli/dct_double.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/debug_print.o: guetzli/debug_print.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/entropy_encode.o: guetzli/entropy_encode.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/fdct.o: guetzli/fdct.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/gamma_correct.o: guetzli/gamma_correct.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/idct.o: guetzli/idct.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_data.o: guetzli/jpeg_data.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_data_decoder.o: guetzli/jpeg_data_decoder.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_data_encoder.o: guetzli/jpeg_data_encoder.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_data_reader.o: guetzli/jpeg_data_reader.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_data_writer.o: guetzli/jpeg_data_writer.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/jpeg_huffman_decode.o: guetzli/jpeg_huffman_decode.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/output_image.o: guetzli/output_image.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/preprocess_downsample.o: guetzli/preprocess_downsample.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/processor.o: guetzli/processor.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/quality.o: guetzli/quality.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/quantize.o: guetzli/quantize.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/score.o: guetzli/score.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/butteraugli.o: third_party/butteraugli/butteraugli/butteraugli.cc @echo $(notdir $<) $(SILENT) $(CXX) $(ALL_CXXFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" -include $(OBJECTS:%.o=%.d) ifneq (,$(PCH)) -include $(OBJDIR)/$(notdir $(PCH)).d endif ================================================ FILE: guetzli_static.vcxproj ================================================  Release x64 Release Win32 Debug x64 Debug Win32 {30B3C385-1C81-B78B-0515-28B2F18193F0} true Win32Proj guetzli_static StaticLibrary false Unicode v140 StaticLibrary false Unicode v140 StaticLibrary true Unicode v140 StaticLibrary true Unicode v140 bin\x86_64\Release\ obj\x86_64\Release\guetzli_static\ guetzli_static .lib bin\x86\Release\ obj\x86\Release\guetzli_static\ guetzli_static .lib bin\x86_64\Debug\ obj\x86_64\Debug\guetzli_static\ guetzli_static .lib bin\x86\Debug\ obj\x86\Debug\guetzli_static\ guetzli_static .lib NotUsing Level3 .;third_party\butteraugli;%(AdditionalIncludeDirectories) Full true true false true Windows true true NotUsing Level3 .;third_party\butteraugli;%(AdditionalIncludeDirectories) Full true true false true Windows true true NotUsing Level3 .;third_party\butteraugli;%(AdditionalIncludeDirectories) EditAndContinue Disabled Windows true NotUsing Level3 .;third_party\butteraugli;%(AdditionalIncludeDirectories) EditAndContinue Disabled Windows true ================================================ FILE: guetzli_static.vcxproj.filters ================================================ {C9FCBE14-35DC-3DB0-3EF4-C886AA52A411} {0FB18DF1-7B66-06E7-045B-00BE700FFDEA} {468B2B32-B2C2-73C9-BBCC-D7EC27839AC2} {FD6FCB41-6929-36EC-F288-50C65E41EC5B} guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli third_party\butteraugli\butteraugli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli guetzli third_party\butteraugli\butteraugli ================================================ FILE: premake5.lua ================================================ workspace "guetzli" configurations { "Release", "Debug" } language "C++" flags { "C++11" } includedirs { ".", "third_party/butteraugli" } filter "action:vs*" platforms { "x86_64", "x86" } filter "platforms:x86" architecture "x86" filter "platforms:x86_64" architecture "x86_64" -- workaround for #41 filter "action:gmake" symbols "On" filter "configurations:Debug" symbols "On" filter "configurations:Release" optimize "Full" filter {} project "guetzli_static" kind "StaticLib" files { "guetzli/*.cc", "guetzli/*.h", "third_party/butteraugli/butteraugli/butteraugli.cc", "third_party/butteraugli/butteraugli/butteraugli.h" } removefiles "guetzli/guetzli.cc" filter "action:gmake" linkoptions { "`pkg-config --static --libs libpng || libpng-config --static --ldflags`" } buildoptions { "`pkg-config --static --cflags libpng || libpng-config --static --cflags`" } project "guetzli" kind "ConsoleApp" filter "action:gmake" linkoptions { "`pkg-config --libs libpng || libpng-config --ldflags`" } buildoptions { "`pkg-config --cflags libpng || libpng-config --cflags`" } filter "action:vs*" links { "shlwapi" } filter {} files { "guetzli/*.cc", "guetzli/*.h", "third_party/butteraugli/butteraugli/butteraugli.cc", "third_party/butteraugli/butteraugli/butteraugli.h" } ================================================ FILE: snapcraft.yaml ================================================ name: guetzli version: master summary: JPEG encoder for excellent compression density at high visual quality description: | Guetzli-generated images are typically 20-30% smaller than images of equivalent quality generated by libjpeg. Guetzli generates only sequential (nonprogressive) JPEGs due to faster decompression speeds they offer. grade: devel confinement: strict apps: guetzli: command: bin/Release/guetzli plugs: [home] parts: guetzli: source: . plugin: make build-packages: [libpng12-dev, libgflags-dev] artifacts: [bin/Release/guetzli] ================================================ FILE: tests/golden_checksums.txt ================================================ 40a8e87fc2feecfe5db466f44c248bb970591132f357140fd3cc93705cd233fb bees-420.jpg.guetzli.jpg 2da4c37bfc1ed94c1285bf43e07b61c647d160179b8c685707bdf6dcc7d28b33 bees.jpg.guetzli.jpg 39cfc110d3d389ddf3b9c47adece290ef077b78467f1ed39679b691c6fb7c96d bees.png.guetzli.jpg 2da4c37bfc1ed94c1285bf43e07b61c647d160179b8c685707bdf6dcc7d28b33 bees-progressive.jpg.guetzli.jpg 80fb7d673090ed5269b680a90d958e876190516b76f12caca7d4bf227ef9da3d bench2-420.jpg.guetzli.jpg 8f76f86500851858c047d03c2fc49841d60a84163656bf45662d653ee58e66b8 bench2.jpg.guetzli.jpg 9d7734214baaf01bcd44ee080ea3b7fef8829f2fa51b53b79a8b6474c0c5ae80 bench2.png.guetzli.jpg 8f76f86500851858c047d03c2fc49841d60a84163656bf45662d653ee58e66b8 bench2-progressive.jpg.guetzli.jpg e2ca9887fbe6c98fc84a2688c3278387ba17a19b4cc481c267044ddc8d16ed55 bench-420.jpg.guetzli.jpg 7cec578c81ab24525dd4fffd793886114d87d80b48a8666795b3b6c400a18bd0 bench.jpg.guetzli.jpg 4aa0a50b196dba81534e31f451517d952cfbdd40b32c9a9c3c26dd1f954216ce bench.png.guetzli.jpg 7cec578c81ab24525dd4fffd793886114d87d80b48a8666795b3b6c400a18bd0 bench-progressive.jpg.guetzli.jpg e2dfa6c9642ef73947f64671d3d288c82595293cfeb6aabe490bda6e63d6e824 bicycles-420.jpg.guetzli.jpg 0dfd25366c2cfe0b93b0ad8da38be623dd067f590968f6dbde66e8fe6b04ceff bicycles.jpg.guetzli.jpg a9439e530c365a62c965e2d858c8de8fbd3f44e6e9c6ade1c2248ad373fb8755 bicycles.png.guetzli.jpg 0dfd25366c2cfe0b93b0ad8da38be623dd067f590968f6dbde66e8fe6b04ceff bicycles-progressive.jpg.guetzli.jpg 8ebf427aa736b66d315cead233b6eb6bc0dc49e83529b0c53315b272d35148fa blue-rose-420.jpg.guetzli.jpg 9ccdf1be8f0d121d4b6888e47d187e8b6378b63d5592a2b9f001a4d017506bde blue-rose.jpg.guetzli.jpg 6c75b537c2d603aa51c9fbc8f7f6b1bff413de269b13eec5982d24cfcf1e0d08 blue-rose.png.guetzli.jpg 9ccdf1be8f0d121d4b6888e47d187e8b6378b63d5592a2b9f001a4d017506bde blue-rose-progressive.jpg.guetzli.jpg 2763977af200403d57c7b9d9d33cfb76947076413c9531b16171c0a5fbc83339 brake-light-420.jpg.guetzli.jpg a84703948373a12cc1db1edd32616c983db08ff07f550a3be7b111bf9e4ba06f brake-light.jpg.guetzli.jpg eda4f537c54ca55eddc03097b6aa57db61c57f80e276306ff4c73b72123e5402 brake-light.png.guetzli.jpg a84703948373a12cc1db1edd32616c983db08ff07f550a3be7b111bf9e4ba06f brake-light-progressive.jpg.guetzli.jpg c25be9699cad0796f1c663d8a7d92c2e99dfb95bed4efd0d58c15d52bb4fe049 cloth-420.jpg.guetzli.jpg 6bf2ef7d27a5a8614db0f698b1f977cd0f486fdc2d4260b90f0d64d653383c35 cloth.jpg.guetzli.jpg 446850f4decd68d77c8a5b09d925fdc9075f44fdfe6dfb2e60a1064677678a3f cloth.png.guetzli.jpg 6bf2ef7d27a5a8614db0f698b1f977cd0f486fdc2d4260b90f0d64d653383c35 cloth-progressive.jpg.guetzli.jpg e6407f3d38f70dde51584ee174ae29f53cc2d2d7e63812a3405d20079d67a45c geranium2-420.jpg.guetzli.jpg 14fb9aab1ebd6d7b8779566665fe2f94b07158b277f29296ef2f9fee71c3c4a4 geranium2.jpg.guetzli.jpg b7ded98029eca0ecf75bb01d5ea54a833fa13377136b4e7e404ae35503f82eb8 geranium2.png.guetzli.jpg 14fb9aab1ebd6d7b8779566665fe2f94b07158b277f29296ef2f9fee71c3c4a4 geranium2-progressive.jpg.guetzli.jpg 9eea5d54068ccaacfb1839c67c401685646c73edfdc38bac8e1e3a084e268f0d geranium-420.jpg.guetzli.jpg 4f249d42280d6f982fa093343236b318be887dc0f2241e125fcf6d4c913305e3 geranium.jpg.guetzli.jpg 31040ac1623c371187c94946ae91c548c553403f60cc43899d9e13bcf39b6a7c geranium.png.guetzli.jpg 4f249d42280d6f982fa093343236b318be887dc0f2241e125fcf6d4c913305e3 geranium-progressive.jpg.guetzli.jpg 86fb6f73a32bac4c7f3dfd009d5f745dd3694bc773bd853278e14624a91f7434 green-420.jpg.guetzli.jpg e1fbdb05fe74f2d78cf6547621d99afea6e72069d8de68da274d66530b5dcdd7 green.jpg.guetzli.jpg 3df6e963406121db078b99653d7c4e49ce2affe99b31212b026239d082748291 green.png.guetzli.jpg e1fbdb05fe74f2d78cf6547621d99afea6e72069d8de68da274d66530b5dcdd7 green-progressive.jpg.guetzli.jpg c2fcd25260b5c52871def4a7ef0136be7e7e7f63f836a974c51a4681a651d7c7 green-rose-420.jpg.guetzli.jpg 90998e98318bb62538fe64f3b60d3100230474bf57dda72cd737eeee8ea482ae green-rose.jpg.guetzli.jpg 513e03accb79e60e9c8a2e9832bdbf9f1af8b23c6905cdb476e0626e1a7009d2 green-rose.png.guetzli.jpg 90998e98318bb62538fe64f3b60d3100230474bf57dda72cd737eeee8ea482ae green-rose-progressive.jpg.guetzli.jpg f1279ca9177e0aea7451bafa4abcd8ecfdf8a939ae97c974fbc802b668d8a56b hand-420.jpg.guetzli.jpg 8d2d8f4a95deea2dca8539a0c12ba8186ea93e7d9bcff9cb2c0bb9eab5504d1f hand.jpg.guetzli.jpg 4d156e4dbec82cb2f8fa324ea9f9142327d63853379cf3332775fdd40bbafc2f hand.png.guetzli.jpg 8d2d8f4a95deea2dca8539a0c12ba8186ea93e7d9bcff9cb2c0bb9eab5504d1f hand-progressive.jpg.guetzli.jpg c3a3f86da0eeacc015504139181c19f874e9631336bb5f90fbf8a367058ec95f lichen-420.jpg.guetzli.jpg 44db143ce962b2eb45fcc1a79468d96ca7c677cba864efd9a984e3f86de5a0a5 lichen.jpg.guetzli.jpg 9df98b8aa95a160a5937b602de715a9c54dde5ea9cd25d188643642656bb22b7 lichen.png.guetzli.jpg 44db143ce962b2eb45fcc1a79468d96ca7c677cba864efd9a984e3f86de5a0a5 lichen-progressive.jpg.guetzli.jpg c8b301b6ecfba6c9e9119b5a59e6174b6e7e9341b96943efd32f5d53c8d5e858 minerology-420.jpg.guetzli.jpg cff08d7d8896f5a4a95f04e34a1af6b6e165672532d828e95ade620beca9026f minerology.jpg.guetzli.jpg 4d789c23515fabc586d7e16c41e56df3013d6512f4bde17a6e8872b37911fcff minerology.png.guetzli.jpg cff08d7d8896f5a4a95f04e34a1af6b6e165672532d828e95ade620beca9026f minerology-progressive.jpg.guetzli.jpg e4ff3c1299f42d6ee537c1122954a5131f48a0d4fe0ef03c220c4c93543964da out-of-focus-420.jpg.guetzli.jpg 57590d333c2970e920fd5239bef84542823a465049aab17a111092863f2126ca out-of-focus.jpg.guetzli.jpg 087e342be60df5851977c9a178bf52460108d169eaab8272c2c83789da44d818 out-of-focus.png.guetzli.jpg 57590d333c2970e920fd5239bef84542823a465049aab17a111092863f2126ca out-of-focus-progressive.jpg.guetzli.jpg dcff545a0bf03441a679134a4fa3922325bd0196447cdf2de63eea48eac7fab3 pimpinelli-420.jpg.guetzli.jpg fccbde44ee9cc867bda3f2d93ae8b47a893ba683a0f0e2aff7c9213c126b4830 pimpinelli.jpg.guetzli.jpg a51ffedd0a9c814a5d763418e523b18c25abf167088f245018196a19c7830583 pimpinelli.png.guetzli.jpg fccbde44ee9cc867bda3f2d93ae8b47a893ba683a0f0e2aff7c9213c126b4830 pimpinelli-progressive.jpg.guetzli.jpg a23ecdeb4a556d25a596fe5d49dde55eb7d4531d999629a610e68953811c678f pink-flower-420.jpg.guetzli.jpg 87ac31333c5a61e00947a2f7f94078d9b753b6a0291d218ca5547861a7c28b57 pink-flower.jpg.guetzli.jpg 773fb2075a222ca7e07bec1c87f7453fc4d4b578e44ee816bc2f90d971706a70 pink-flower.png.guetzli.jpg 87ac31333c5a61e00947a2f7f94078d9b753b6a0291d218ca5547861a7c28b57 pink-flower-progressive.jpg.guetzli.jpg aceb338115241d9984510fd2e8a2bf46b3c5fc431e827a2d1efe496dff038675 port-420.jpg.guetzli.jpg 02aac145b6df57db2913952d3d46c8d456e2f000cff1ff4bfc574b27175335e0 port.jpg.guetzli.jpg 157a25812bcf7bce343fe6c6a88932ee49117b46b5d3ba0aa3421edc8f2f1a09 port.png.guetzli.jpg 02aac145b6df57db2913952d3d46c8d456e2f000cff1ff4bfc574b27175335e0 port-progressive.jpg.guetzli.jpg f41f613ecfae42d050115b785c1591724fcb7937c361b9c5d2a3248b7580953f rainbow-420.jpg.guetzli.jpg 74d94a13c52b0d582c50d6bc70cecb6762c08740db6c234dff9b0e1c04fccbb5 rainbow.jpg.guetzli.jpg 657efb5cfa742fbdfd6304703b131a63c2ddf8b686600840a800e7d94b4da0eb rainbow.png.guetzli.jpg 74d94a13c52b0d582c50d6bc70cecb6762c08740db6c234dff9b0e1c04fccbb5 rainbow-progressive.jpg.guetzli.jpg 4667bf8db85507bf260bdbad439b87250e0613092131e0a662f22164d7abb91e red-flowers-420.jpg.guetzli.jpg d48d6208e857fc92e9fb584d19e5738d767b520d3fd57937f16657a4cfe57743 red-flowers.jpg.guetzli.jpg 6a2fb22da73b20e80d31fd2920d16c847cc0f68eb44af3f924766339f8869a99 red-flowers.png.guetzli.jpg d48d6208e857fc92e9fb584d19e5738d767b520d3fd57937f16657a4cfe57743 red-flowers-progressive.jpg.guetzli.jpg 37827b7701fcb66ee2d808f5c7a5be5f6fbe732241f178f152b12103e3581d35 red-room-420.jpg.guetzli.jpg ed14d00d18dc1b810ad595db079ce8897d00afc39e9cd09dcc122eb8c9ab59f6 red-room.jpg.guetzli.jpg b1ca1634719fda14c88dea08f99fab9cd5450cc41dd32385387fd774e33896ce red-room.png.guetzli.jpg ed14d00d18dc1b810ad595db079ce8897d00afc39e9cd09dcc122eb8c9ab59f6 red-room-progressive.jpg.guetzli.jpg c5499fdc97b3ae02d77ea12140d6da8ad645406e66adc88250a3c980bb70fe7d red-rose-420.jpg.guetzli.jpg f9a97e475af9127ea6d6d4d41fec52330ca075aae707185d90910fe198695e8d red-rose.jpg.guetzli.jpg 22f21955e7078745d03c1eb1985b8c5ffbd0b615870071a821102c44bd94af97 red-rose.png.guetzli.jpg f9a97e475af9127ea6d6d4d41fec52330ca075aae707185d90910fe198695e8d red-rose-progressive.jpg.guetzli.jpg 4df6d9b244c2d02cacff35ada998da3be13d1c9f5e42d4a2ab9b4725fc78dfa5 rgb-420.jpg.guetzli.jpg 19256b30557be9dc6a7effe6418f2c1ba6e624940ef1f41c0ca71e356963014c rgb.jpg.guetzli.jpg c1f8e4161a8b6baddea1d279f4d490670560d9c5d1161b66ee101c4250d8dd48 rgb.png.guetzli.jpg 19256b30557be9dc6a7effe6418f2c1ba6e624940ef1f41c0ca71e356963014c rgb-progressive.jpg.guetzli.jpg a2eccf2d9e63c830f6666b7082589e5e58f551af460277110b0a21f07bc8e6d6 station-420.jpg.guetzli.jpg 0d5a61eaeda6616f026bea9789620b91c47d4c4b099099d28c76708c9e9caf8f station.jpg.guetzli.jpg 3cf674f2687bee61e43ef0e57bcec15d93a945c14e4413d22c1a0ee13d64f4f2 station.png.guetzli.jpg 0d5a61eaeda6616f026bea9789620b91c47d4c4b099099d28c76708c9e9caf8f station-progressive.jpg.guetzli.jpg 632c1b8aa116847dbb3471fce2e7201eb2bd028f22ebc920ae97344bfaf8c263 stp2-420.jpg.guetzli.jpg 6e741f8d6f27c4aec93d3bd01216b28aac31f7f8f501e0cbb49249ba02e3b82a stp2.jpg.guetzli.jpg 976fd6eca7e49b097817a1424a41ba71c8d3edbdb3fe9b55db4a4a36404c0b34 stp2.png.guetzli.jpg 6e741f8d6f27c4aec93d3bd01216b28aac31f7f8f501e0cbb49249ba02e3b82a stp2-progressive.jpg.guetzli.jpg a817caaf39ca100eb14ab96295298e9ce744e9262d6e678d31e86d2990ac5c2d stp-420.jpg.guetzli.jpg b7d1652051ed7ee6e07cb4b8918ab92042573746cd5a8fd1342aeaefc3e11c26 stp.jpg.guetzli.jpg 126d2d08e936828e38d70543c576fc81021aba7010a8368842abfcd777672e9a stp.png.guetzli.jpg b7d1652051ed7ee6e07cb4b8918ab92042573746cd5a8fd1342aeaefc3e11c26 stp-progressive.jpg.guetzli.jpg cf30b2be770b1cf597cae579d7d9e027586e0b53eb022d9f875bc56152c44cd4 vflower-420.jpg.guetzli.jpg c65d52efb093cb3764dce7efdf6320728faf5fbb48545ec7de107b505abd00cb vflower.jpg.guetzli.jpg 29f303b8354443478b6f209b6e250ab40740b58cbcee34bc5669b0eb8da4ee2b vflower.png.guetzli.jpg c65d52efb093cb3764dce7efdf6320728faf5fbb48545ec7de107b505abd00cb vflower-progressive.jpg.guetzli.jpg 6c53f97edc667a5d871045fa97bd372c2a84a94a126f5d5d25d6ffadc97574c7 white-yellow-420.jpg.guetzli.jpg 7b83bd4ab679f721bc067ce86e2375c355265bb1f01668efc47423f0c707cd0b white-yellow.jpg.guetzli.jpg 62759933c7b505e531fda54bf863ad494b09a9c91262c43a715777658d29fee7 white-yellow.png.guetzli.jpg 7b83bd4ab679f721bc067ce86e2375c355265bb1f01668efc47423f0c707cd0b white-yellow-progressive.jpg.guetzli.jpg f0cea391185003b58594a9d4e43226be4e6e68ed882ebee8e0787fc129613d74 wollerau-420.jpg.guetzli.jpg afe66a4d5b26180cc2471aac20253f7f7a0064fb458bdbc8a4376819779d126f wollerau.jpg.guetzli.jpg ed223e0974fa0030ad83c20b4616105355efd5d3360fbb496956ad3d4f9e59e8 wollerau.png.guetzli.jpg afe66a4d5b26180cc2471aac20253f7f7a0064fb458bdbc8a4376819779d126f wollerau-progressive.jpg.guetzli.jpg a9f72e6805afa346e8dff15d9f113d5cf637a4a11c670b4454c9a1dedd1824f8 yellow2-420.jpg.guetzli.jpg 62295c47a2abbebd89d56ad9cbdb85fa4f3e80781bee69977058b7869de46b5c yellow2.jpg.guetzli.jpg 5be448a421e9fef23e054a683bef8e3fe19cd968a72d250944050b176600e540 yellow2.png.guetzli.jpg 62295c47a2abbebd89d56ad9cbdb85fa4f3e80781bee69977058b7869de46b5c yellow2-progressive.jpg.guetzli.jpg d268fb8e60412f56689f473da391330c594d661b80af9bd46dbfbfed7a40d5f3 yellow-420.jpg.guetzli.jpg 31a33c73f06c4b5b3abdfafff7a50e1b107b013802858a3dc405a59d333b6b8b yellow.jpg.guetzli.jpg ee5b2844b2b23ad50434539ae31ec4b3ea027595fd959d192c03553bf167ae24 yellow.png.guetzli.jpg 31a33c73f06c4b5b3abdfafff7a50e1b107b013802858a3dc405a59d333b6b8b yellow-progressive.jpg.guetzli.jpg ================================================ FILE: tests/golden_test.sh ================================================ #!/bin/bash GUETZLI=${1:-bin/Release/guetzli} INPUT_DIR_PNG=$HOME/.cache/guetzli-test-corpus INPUT_DIR_JPG=$(mktemp -d) OUTPUT_DIR=$(mktemp -d) if [[ -d $INPUT_DIR_PNG ]]; then (cd $INPUT_DIR_PNG ; sha256sum -c ; exit $? ) < $(dirname $0)/png_checksums.txt || rm -r ${INPUT_DIR_PNG} fi if [[ ! -d $INPUT_DIR_PNG ]]; then mkdir -p $INPUT_DIR_PNG || exit 2 curl https://storage.googleapis.com/test-image-corpus/test-corpus.tgz | tar -C $INPUT_DIR_PNG -zxf - || exit 2 fi for i in $INPUT_DIR_PNG/*.png; do pngtopnm < $i | cjpeg -sample 1x1 -quality 100 > $INPUT_DIR_JPG/$(basename $i .png).jpg || exit 2 pngtopnm < $i | cjpeg -sample 1x1 -progressive -quality 100 > $INPUT_DIR_JPG/$(basename $i .png)-progressive.jpg || exit 2 pngtopnm < $i | cjpeg -sample 2x2,1x1,1x1 -quality 100 > $INPUT_DIR_JPG/$(basename $i .png)-420.jpg || exit 2 done for i in $INPUT_DIR_PNG/*.png $INPUT_DIR_JPG/*.jpg; do echo $i $OUTPUT_DIR/$(basename $i).guetzli.jpg done | xargs -L 1 -P $(getconf _NPROCESSORS_ONLN) -t $GUETZLI if [[ -n "$UPDATE_GOLDEN" ]]; then (cd $OUTPUT_DIR ; sha256sum *) > $(dirname $0)/golden_checksums.txt else (cd $OUTPUT_DIR ; sha256sum -c) < $(dirname $0)/golden_checksums.txt fi ================================================ FILE: tests/png_checksums.txt ================================================ 3531f6d5d30faf9d781f444398f32495a94ad276f2654c579a489bb2014f156b bees.png 0210aa8ed8d044e10b6e816ef0f6603de1d5834a1d0d2027898fafdc7bcac396 bench2.png e1fe02c671620555d25ee4e3ed8d16a216a40d52edf1b73c0d5088cee1b19da5 bench.png 616a52b544630ed7a6b8db33ca12c1bd6aad4ca07faf8f658cf4d1981af5b0bd bicycles.png 43252838b12e987502dfc98446e474a9b1008a6a49987ea6f6692eee68d3f541 blue-rose.png 8f6a52fb6c3ffb1b2c32e47075c7e2c22975570f0da0694c4272e1cec4c05438 brake-light.png 7572fc7bb317838b3ad0a7e39154e92c04f56c172ea347e85752146da2978578 cloth.png eb7797c8929576ccd8acc27907a3f354ae19246a58d6f02279918be541359692 geranium2.png d0f99e29280546f75b2065590ef3d9bed7ba86c3f1e2fb403971c83ebc996594 geranium.png c1f96c117f607205773ce5339ee2a1076b8f99de4bf674ee0aae25984f39c243 green.png 7564f4e4693ac423581e1d496fe339dd13cbfc6b1393c0d9e8c5bd4a1f53a8e8 green-rose.png b5236607cdce8f88e42132f6760c1b61a8dc83d975cd44a0be40684eaf035294 hand.png a784868d51b5a1d170fa1ae733cced48e1fc30f0bf42c81e6a9fd664e462bb83 lichen.png ab0eff8e6b57694455bffa91e74c91a0507e61294032521caacc28128ad3cf90 minerology.png d30b95585fe03802b9d9a70d62f8ce3b7b84f1072de67efabf59b8a83b4d9eae out-of-focus.png a6e05977e62e39d5faa7154fe00d2aaa3d18f28b9f933ccd8906c2f8a1f11633 pimpinelli.png 334d783039d60b805f46a2e9e4667749bc64ccdd97bb356e5faa045311861efb pink-flower.png 390ecd976e1e73f46addd6f245efe9fd886fed7437cbf5049db02e2f6f2d6afd port.png 858018895b71dd3cc612c3939832193c2d656b2ae1a8aec7a38c2f41f61d7005 rainbow.png d81b49b0a4db09aab24f573c141899955391440ccfdfa57c060eafa76c3416a4 red-flowers.png 0ebf4139eece6a5fdaf5397ed64c7fb8b9115a607ae7f82b2e4bf56ae0405f99 red-room.png 82a845a65b2c7217f7baea1d6b63f466d70dec5783ebb4b6b41f060348d5bfb3 red-rose.png 37b9c33b7ee63788ad5f09eb8f53127ebb2fc9d9113b856e38c440fe8a78df05 rgb.png 7dae648bb5f9bf22bb3c1f51f94ef1a04ec20b981c9d5b01f42f364d5ab63c11 station.png 8241aa8f95a8b051936cd519e0205a67a9d1f48e229c6c5745575adf68f4a3ab stp2.png 03c0139181f9212e188348e2315caf73a54e213f03e872f04759ef2936f851dc stp.png 4938aa4a4527df8deecd10c4007d4a63ff8d4f81f63b9fd87c471a0b5437a50c vflower.png ada6424a72548e7b80ac58590b58d1b7a7e185ef87ebdf76841b2a573b9cf23a white-yellow.png 77a5d289b2fa19b0f72de03cad2ce311c8b3cb088a32928811b2287fbeb18ce9 wollerau.png f1dbf76f4bb7b7e79c98fd1e8572f5b9a01d1a6c0ea8a1e6361dc2b8188f094e yellow2.png 37ccbd18a347ba4d326fb8d9ae56fbadaaefca9bb5a2298b9d09f30c18616ae6 yellow.png ================================================ FILE: tests/smoke_test.sh ================================================ #!/bin/bash GUETZLI=${1:-bin/Release/guetzli} BEES_PNG=$(dirname $0)/bees.png BEES_JPG=$(mktemp ${TMPDIR:-/tmp}/beesXXXX.jpg) BUTTERAUGLI=$2 pngtopnm < $BEES_PNG | cjpeg -sample 1x1 -quality 100 > $BEES_JPG || exit 2 function run_test() { # png/jpeg stdin/file stdout/file flags... local in= local out=$(mktemp ${TMPDIR:-/tmp}/beesXXX.guetzli.jpg) echo "Testing $@, output in $out" case "$1" in png) in=$BEES_PNG ;; jpeg) in=$BEES_JPG ;; *) exit 2 ;; esac shift local inouthandling="$1:$2" shift; shift case "$inouthandling" in file:file) $GUETZLI $@ $in $out ;; stdin:file) $GUETZLI $@ - $out < $in ;; file:stdout) $GUETZLI $@ $in - > $out ;; stdin:stdout) $GUETZLI $@ - - < $in > $out ;; *) exit 2 ;; esac test -f "$out" || { echo "$out doesn't exist"; exit 1; } djpeg < $out > /dev/null || { echo "$out is not a valid JPEG"; exit 1; } if [ -n "$BUTTERAUGLI" ]; then $BUTTERAUGLI $in $out fi rm $out echo "OK" } run_test png file file run_test png stdin file run_test jpeg file file run_test jpeg stdin file run_test png stdin stdout run_test png file stdout run_test png file stdout --verbose run_test png file stdout --nomemlimit run_test png file stdout --memlimit 100 run_test png file stdout --quality 85 echo $GUETZLI /dev/null /dev/null $GUETZLI /dev/null /dev/null if [[ $? -ne 1 ]]; then echo "Expected a clean failure" exit 1 fi ================================================ FILE: third_party/butteraugli/BUILD ================================================ cc_library( name = "butteraugli_lib", srcs = [ "butteraugli/butteraugli.cc", "butteraugli/butteraugli.h", ], hdrs = [ "butteraugli/butteraugli.h", ], copts = ["-Wno-sign-compare"], visibility = ["//visibility:public"], ) cc_binary( name = "butteraugli", srcs = ["butteraugli/butteraugli_main.cc"], copts = ["-Wno-sign-compare"], visibility = ["//visibility:public"], deps = [ ":butteraugli_lib", "@jpeg_archive//:jpeg", "@png_archive//:png", ], ) ================================================ FILE: third_party/butteraugli/LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: third_party/butteraugli/README.md ================================================ # butteraugli > A tool for measuring perceived differences between images ## Introduction Butteraugli is a project that estimates the psychovisual similarity of two images. It gives a score for the images that is reliable in the domain of barely noticeable differences. Butteraugli not only gives a scalar score, but also computes a spatial map of the level of differences. One of the main motivations for this project is the statistical differences in location and density of different color receptors, particularly the low density of blue cones in the fovea. Another motivation comes from more accurate modeling of ganglion cells, particularly the frequency space inhibition. ## Use Butteraugli can work as a quality metric for lossy image and video compression. On our small test corpus butteraugli performs better than our implementations of the reference methods, psnrhsv-m, ssim, and our yuv-color-space variant of ssim. One possible use is to define the quality level setting used in a jpeg compressor, or to compare two or more compression methods at the same level of psychovisual differences. Butteraugli is intended to be a research tool more than a practical tool for choosing compression formats. We don't know how well butteraugli performs with major deformations -- we have mostly tuned it within a small range of quality, roughly corresponding to jpeg qualities 90 to 95. ## Interface Only a C++ interface is provided. The interface takes two images and outputs a map together with a scalar value defining the difference. The scalar value can be compared to two reference values that divide the value space into three experience classes: 'great', 'acceptable' and 'not acceptable'. ## Build instructions Install [Bazel](http://bazel.build) by following the [instructions](https://www.bazel.build/docs/install.html). Run `bazel build -c opt //:butteraugli` in the directory that contains this README file to build the [command-line utility](#cmdline-tool). If you want to use Butteraugli as a library, depend on the `//:butteraugli_lib` target. Alternatively, you can use the Makefile provided in the `butteraugli` directory, after ensuring that [libpng](http://www.libpng.org/) and [libjpeg](http://ijg.org/) are installed. On some systems you might need to also install corresponding `-dev` packages. The code is portable and also compiles on Windows after defining `_CRT_SECURE_NO_WARNINGS` in the project settings. ## Command-line utility {#cmdline-tool} Butteraugli, apart from the library, comes bundled with a comparison tool. The comparison tool supports PNG and JPG images as inputs. To compare images, run: ``` butteraugli image1.{png|jpg} image2.{png|jpg} ``` The tool can also produce a heatmap of differences between images. The heatmap will be output as a PNM image. To produce one, run: ``` butteraugli image1.{png|jpg} image2.{png|jpg} heatmap.pnm ``` ================================================ FILE: third_party/butteraugli/WORKSPACE ================================================ workspace(name = "butteraugli") new_http_archive( name = "png_archive", url = "http://github.com/glennrp/libpng/archive/v1.2.57.zip", sha256 = "a941dc09ca00148fe7aaf4ecdd6a67579c293678ed1e1cf633b5ffc02f4f8cf7", strip_prefix = "libpng-1.2.57", build_file = "png.BUILD", ) new_http_archive( name = "zlib_archive", url = "http://zlib.net/fossils/zlib-1.2.10.tar.gz", sha256 = "8d7e9f698ce48787b6e1c67e6bff79e487303e66077e25cb9784ac8835978017", strip_prefix = "zlib-1.2.10", build_file = "zlib.BUILD", ) new_http_archive( name = "jpeg_archive", url = "http://www.ijg.org/files/jpegsrc.v9b.tar.gz", sha256 = "240fd398da741669bf3c90366f58452ea59041cacc741a489b99f2f6a0bad052", strip_prefix = "jpeg-9b", build_file = "jpeg.BUILD", ) ================================================ FILE: third_party/butteraugli/butteraugli/Makefile ================================================ LDLIBS += -lpng -ljpeg CXXFLAGS += -std=c++11 -I.. LINK.o = $(LINK.cc) all: butteraugli.o butteraugli_main.o butteraugli butteraugli: butteraugli.o butteraugli_main.o ================================================ FILE: third_party/butteraugli/butteraugli/butteraugli.cc ================================================ // Copyright 2016 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Author: Jyrki Alakuijala (jyrki.alakuijala@gmail.com) // // The physical architecture of butteraugli is based on the following naming // convention: // * Opsin - dynamics of the photosensitive chemicals in the retina // with their immediate electrical processing // * Xyb - hybrid opponent/trichromatic color space // x is roughly red-subtract-green. // y is yellow. // b is blue. // Xyb values are computed from Opsin mixing, not directly from rgb. // * Mask - for visual masking // * Hf - color modeling for spatially high-frequency features // * Lf - color modeling for spatially low-frequency features // * Diffmap - to cluster and build an image of error between the images // * Blur - to hold the smoothing code #include "butteraugli/butteraugli.h" #include #include #include #include #include #include #include // Restricted pointers speed up Convolution(); MSVC uses a different keyword. #ifdef _MSC_VER #define __restrict__ __restrict #endif #ifndef PROFILER_ENABLED #define PROFILER_ENABLED 0 #endif #if PROFILER_ENABLED #else #define PROFILER_FUNC #define PROFILER_ZONE(name) #endif namespace butteraugli { void *CacheAligned::Allocate(const size_t bytes) { char *const allocated = static_cast(malloc(bytes + kCacheLineSize)); if (allocated == nullptr) { return nullptr; } const uintptr_t misalignment = reinterpret_cast(allocated) & (kCacheLineSize - 1); // malloc is at least kPointerSize aligned, so we can store the "allocated" // pointer immediately before the aligned memory. assert(misalignment % kPointerSize == 0); char *const aligned = allocated + kCacheLineSize - misalignment; memcpy(aligned - kPointerSize, &allocated, kPointerSize); return BUTTERAUGLI_ASSUME_ALIGNED(aligned, 64); } void CacheAligned::Free(void *aligned_pointer) { if (aligned_pointer == nullptr) { return; } char *const aligned = static_cast(aligned_pointer); assert(reinterpret_cast(aligned) % kCacheLineSize == 0); char *allocated; memcpy(&allocated, aligned - kPointerSize, kPointerSize); assert(allocated <= aligned - kPointerSize); assert(allocated >= aligned - kCacheLineSize); free(allocated); } static inline bool IsNan(const float x) { uint32_t bits; memcpy(&bits, &x, sizeof(bits)); const uint32_t bitmask_exp = 0x7F800000; return (bits & bitmask_exp) == bitmask_exp && (bits & 0x7FFFFF); } static inline bool IsNan(const double x) { uint64_t bits; memcpy(&bits, &x, sizeof(bits)); return (0x7ff0000000000001ULL <= bits && bits <= 0x7fffffffffffffffULL) || (0xfff0000000000001ULL <= bits && bits <= 0xffffffffffffffffULL); } static inline void CheckImage(const ImageF &image, const char *name) { for (size_t y = 0; y < image.ysize(); ++y) { const float * const BUTTERAUGLI_RESTRICT row = image.Row(y); for (size_t x = 0; x < image.xsize(); ++x) { if (IsNan(row[x])) { printf("Image %s @ %lu,%lu (of %lu,%lu)\n", name, x, y, image.xsize(), image.ysize()); exit(1); } } } } #if BUTTERAUGLI_ENABLE_CHECKS #define CHECK_NAN(x, str) \ do { \ if (IsNan(x)) { \ printf("%d: %s\n", __LINE__, str); \ abort(); \ } \ } while (0) #define CHECK_IMAGE(image, name) CheckImage(image, name) #else #define CHECK_NAN(x, str) #define CHECK_IMAGE(image, name) #endif // Purpose of kInternalGoodQualityThreshold: // Normalize 'ok' image degradation to 1.0 across different versions of // butteraugli. static const double kInternalGoodQualityThreshold = 20.35; static const double kGlobalScale = 1.0 / kInternalGoodQualityThreshold; inline float DotProduct(const float u[3], const float v[3]) { return u[0] * v[0] + u[1] * v[1] + u[2] * v[2]; } std::vector ComputeKernel(float sigma) { const float m = 2.25; // Accuracy increases when m is increased. const float scaler = -1.0 / (2 * sigma * sigma); const int diff = std::max(1, m * fabs(sigma)); std::vector kernel(2 * diff + 1); for (int i = -diff; i <= diff; ++i) { kernel[i + diff] = exp(scaler * i * i); } return kernel; } void ConvolveBorderColumn( const ImageF& in, const std::vector& kernel, const float weight_no_border, const float border_ratio, const size_t x, float* const BUTTERAUGLI_RESTRICT row_out) { const int offset = kernel.size() / 2; int minx = x < offset ? 0 : x - offset; int maxx = std::min(in.xsize() - 1, x + offset); float weight = 0.0f; for (int j = minx; j <= maxx; ++j) { weight += kernel[j - x + offset]; } // Interpolate linearly between the no-border scaling and border scaling. weight = (1.0f - border_ratio) * weight + border_ratio * weight_no_border; float scale = 1.0f / weight; for (size_t y = 0; y < in.ysize(); ++y) { const float* const BUTTERAUGLI_RESTRICT row_in = in.Row(y); float sum = 0.0f; for (int j = minx; j <= maxx; ++j) { sum += row_in[j] * kernel[j - x + offset]; } row_out[y] = sum * scale; } } // Computes a horizontal convolution and transposes the result. ImageF Convolution(const ImageF& in, const std::vector& kernel, const float border_ratio) { ImageF out(in.ysize(), in.xsize()); const int len = kernel.size(); const int offset = kernel.size() / 2; float weight_no_border = 0.0f; for (int j = 0; j < len; ++j) { weight_no_border += kernel[j]; } float scale_no_border = 1.0f / weight_no_border; const int border1 = in.xsize() <= offset ? in.xsize() : offset; const int border2 = in.xsize() - offset; std::vector scaled_kernel = kernel; for (int i = 0; i < scaled_kernel.size(); ++i) { scaled_kernel[i] *= scale_no_border; } // left border for (int x = 0; x < border1; ++x) { ConvolveBorderColumn(in, kernel, weight_no_border, border_ratio, x, out.Row(x)); } // middle for (size_t y = 0; y < in.ysize(); ++y) { const float* const BUTTERAUGLI_RESTRICT row_in = in.Row(y); for (int x = border1; x < border2; ++x) { const int d = x - offset; float* const BUTTERAUGLI_RESTRICT row_out = out.Row(x); float sum = 0.0f; for (int j = 0; j < len; ++j) { sum += row_in[d + j] * scaled_kernel[j]; } row_out[y] = sum; } } // right border for (int x = border2; x < in.xsize(); ++x) { ConvolveBorderColumn(in, kernel, weight_no_border, border_ratio, x, out.Row(x)); } return out; } // A blur somewhat similar to a 2D Gaussian blur. // See: https://en.wikipedia.org/wiki/Gaussian_blur ImageF Blur(const ImageF& in, float sigma, float border_ratio) { std::vector kernel = ComputeKernel(sigma); return Convolution(Convolution(in, kernel, border_ratio), kernel, border_ratio); } // Clamping linear interpolator. inline double InterpolateClampNegative(const double *array, int size, double ix) { if (ix < 0) { ix = 0; } int baseix = static_cast(ix); double res; if (baseix >= size - 1) { res = array[size - 1]; } else { double mix = ix - baseix; int nextix = baseix + 1; res = array[baseix] + mix * (array[nextix] - array[baseix]); } return res; } double GammaMinArg() { double out0, out1, out2; OpsinAbsorbance(0.0, 0.0, 0.0, &out0, &out1, &out2); return std::min(out0, std::min(out1, out2)); } double GammaMaxArg() { double out0, out1, out2; OpsinAbsorbance(255.0, 255.0, 255.0, &out0, &out1, &out2); return std::max(out0, std::max(out1, out2)); } double SimpleGamma(double v) { static const double kGamma = 0.372322653176; static const double limit = 37.8000499603; double bright = v - limit; if (bright >= 0) { static const double mul = 0.0950819040934; v -= bright * mul; } { static const double limit2 = 74.6154406429; double bright2 = v - limit2; if (bright2 >= 0) { static const double mul = 0.01; v -= bright2 * mul; } } { static const double limit2 = 82.8505938033; double bright2 = v - limit2; if (bright2 >= 0) { static const double mul = 0.0316722592629; v -= bright2 * mul; } } { static const double limit2 = 92.8505938033; double bright2 = v - limit2; if (bright2 >= 0) { static const double mul = 0.221249885752; v -= bright2 * mul; } } { static const double limit2 = 102.8505938033; double bright2 = v - limit2; if (bright2 >= 0) { static const double mul = 0.0402547853939; v -= bright2 * mul; } } { static const double limit2 = 112.8505938033; double bright2 = v - limit2; if (bright2 >= 0) { static const double mul = 0.021471798711500003; v -= bright2 * mul; } } static const double offset = 0.106544447664; static const double scale = 10.7950943969; double retval = scale * (offset + pow(v, kGamma)); return retval; } static inline double Gamma(double v) { //return SimpleGamma(v); return GammaPolynomial(v); } std::vector OpsinDynamicsImage(const std::vector& rgb) { PROFILER_FUNC; std::vector xyb(3); std::vector blurred(3); const double kSigma = 1.2; for (int i = 0; i < 3; ++i) { xyb[i] = ImageF(rgb[i].xsize(), rgb[i].ysize()); blurred[i] = Blur(rgb[i], kSigma, 0.0f); } for (size_t y = 0; y < rgb[0].ysize(); ++y) { const float* const BUTTERAUGLI_RESTRICT row_r = rgb[0].Row(y); const float* const BUTTERAUGLI_RESTRICT row_g = rgb[1].Row(y); const float* const BUTTERAUGLI_RESTRICT row_b = rgb[2].Row(y); const float* const BUTTERAUGLI_RESTRICT row_blurred_r = blurred[0].Row(y); const float* const BUTTERAUGLI_RESTRICT row_blurred_g = blurred[1].Row(y); const float* const BUTTERAUGLI_RESTRICT row_blurred_b = blurred[2].Row(y); float* const BUTTERAUGLI_RESTRICT row_out_x = xyb[0].Row(y); float* const BUTTERAUGLI_RESTRICT row_out_y = xyb[1].Row(y); float* const BUTTERAUGLI_RESTRICT row_out_b = xyb[2].Row(y); for (size_t x = 0; x < rgb[0].xsize(); ++x) { float sensitivity[3]; { // Calculate sensitivity based on the smoothed image gamma derivative. float pre_mixed0, pre_mixed1, pre_mixed2; OpsinAbsorbance(row_blurred_r[x], row_blurred_g[x], row_blurred_b[x], &pre_mixed0, &pre_mixed1, &pre_mixed2); // TODO: use new polynomial to compute Gamma(x)/x derivative. sensitivity[0] = Gamma(pre_mixed0) / pre_mixed0; sensitivity[1] = Gamma(pre_mixed1) / pre_mixed1; sensitivity[2] = Gamma(pre_mixed2) / pre_mixed2; } float cur_mixed0, cur_mixed1, cur_mixed2; OpsinAbsorbance(row_r[x], row_g[x], row_b[x], &cur_mixed0, &cur_mixed1, &cur_mixed2); cur_mixed0 *= sensitivity[0]; cur_mixed1 *= sensitivity[1]; cur_mixed2 *= sensitivity[2]; RgbToXyb(cur_mixed0, cur_mixed1, cur_mixed2, &row_out_x[x], &row_out_y[x], &row_out_b[x]); } } return xyb; } // Make area around zero less important (remove it). static BUTTERAUGLI_INLINE float RemoveRangeAroundZero(float w, float x) { return x > w ? x - w : x < -w ? x + w : 0.0f; } // Make area around zero more important (2x it until the limit). static BUTTERAUGLI_INLINE float AmplifyRangeAroundZero(float w, float x) { return x > w ? x + w : x < -w ? x - w : 2.0f * x; } // XybLowFreqToVals converts from low-frequency XYB space to the 'vals' space. // Vals space can be converted to L2-norm space (Euclidean and normalized) // through visual masking. template BUTTERAUGLI_INLINE void XybLowFreqToVals(const V &x, const V &y, const V &b_arg, V *BUTTERAUGLI_RESTRICT valx, V *BUTTERAUGLI_RESTRICT valy, V *BUTTERAUGLI_RESTRICT valb) { static const double xmuli = 5.57547552483; static const double ymuli = 1.20828034498; static const double bmuli = 6.08319517575; static const double y_to_b_muli = -0.628811683685; const V xmul(xmuli); const V ymul(ymuli); const V bmul(bmuli); const V y_to_b_mul(y_to_b_muli); const V b = b_arg + y_to_b_mul * y; *valb = b * bmul; *valx = x * xmul; *valy = y * ymul; } static ImageF SuppressInBrightAreas(size_t xsize, size_t ysize, double mul, double mul2, double reg, const ImageF& hf, const ImageF& brightness) { ImageF inew(xsize, ysize); for (size_t y = 0; y < ysize; ++y) { const float* const rowhf = hf.Row(y); const float* const rowbr = brightness.Row(y); float* const rownew = inew.Row(y); for (size_t x = 0; x < xsize; ++x) { float v = rowhf[x]; float scaler = mul * reg / (reg + rowbr[x]); rownew[x] = scaler * v; } } return inew; } static float SuppressHfInBrightAreas(float hf, float brightness, float mul, float reg) { float scaler = mul * reg / (reg + brightness); return scaler * hf; } static float SuppressUhfInBrightAreas(float hf, float brightness, float mul, float reg) { float scaler = mul * reg / (reg + brightness); return scaler * hf; } static float MaximumClamp(float v, float maxval) { static const double kMul = 0.688059627878; if (v >= maxval) { v -= maxval; v *= kMul; v += maxval; } else if (v < -maxval) { v += maxval; v *= kMul; v -= maxval; } return v; } static ImageF MaximumClamping(size_t xsize, size_t ysize, const ImageF& ix, double yw) { static const double kMul = 0.688059627878; ImageF inew(xsize, ysize); for (size_t y = 0; y < ysize; ++y) { const float* const rowx = ix.Row(y); float* const rownew = inew.Row(y); for (size_t x = 0; x < xsize; ++x) { double v = rowx[x]; if (v >= yw) { v -= yw; v *= kMul; v += yw; } else if (v < -yw) { v += yw; v *= kMul; v -= yw; } rownew[x] = v; } } return inew; } static ImageF SuppressXByY(size_t xsize, size_t ysize, const ImageF& ix, const ImageF& iy, const double yw) { static const double s = 0.745954517135; ImageF inew(xsize, ysize); for (size_t y = 0; y < ysize; ++y) { const float* const rowx = ix.Row(y); const float* const rowy = iy.Row(y); float* const rownew = inew.Row(y); for (size_t x = 0; x < xsize; ++x) { const double xval = rowx[x]; const double yval = rowy[x]; const double scaler = s + (yw * (1.0 - s)) / (yw + yval * yval); rownew[x] = scaler * xval; } } return inew; } static void SeparateFrequencies( size_t xsize, size_t ysize, const std::vector& xyb, PsychoImage &ps) { PROFILER_FUNC; ps.lf.resize(3); // XYB ps.mf.resize(3); // XYB ps.hf.resize(2); // XY ps.uhf.resize(2); // XY // Extract lf ... static const double kSigmaLf = 7.46953768697; static const double kSigmaHf = 3.734768843485; static const double kSigmaUhf = 1.8673844217425; // At borders we move some more of the energy to the high frequency // parts, because there can be unfortunate continuations in tiling // background color etc. So we want to represent the borders with // some more accuracy. static double border_lf = -0.00457628248637; static double border_mf = -0.271277366628; static double border_hf = 0.147068973249; for (int i = 0; i < 3; ++i) { ps.lf[i] = Blur(xyb[i], kSigmaLf, border_lf); // ... and keep everything else in mf. ps.mf[i] = ImageF(xsize, ysize); for (size_t y = 0; y < ysize; ++y) { for (size_t x = 0; x < xsize; ++x) { ps.mf[i].Row(y)[x] = xyb[i].Row(y)[x] - ps.lf[i].Row(y)[x]; } } if (i == 2) { ps.mf[i] = Blur(ps.mf[i], kSigmaHf, border_mf); break; } // Divide mf into mf and hf. ps.hf[i] = ImageF(xsize, ysize); for (size_t y = 0; y < ysize; ++y) { float* BUTTERAUGLI_RESTRICT const row_mf = ps.mf[i].Row(y); float* BUTTERAUGLI_RESTRICT const row_hf = ps.hf[i].Row(y); for (size_t x = 0; x < xsize; ++x) { row_hf[x] = row_mf[x]; } } ps.mf[i] = Blur(ps.mf[i], kSigmaHf, border_mf); static const double w0 = 0.120079806822; static const double w1 = 0.03430529365; if (i == 0) { for (size_t y = 0; y < ysize; ++y) { float* BUTTERAUGLI_RESTRICT const row_mf = ps.mf[0].Row(y); float* BUTTERAUGLI_RESTRICT const row_hf = ps.hf[0].Row(y); for (size_t x = 0; x < xsize; ++x) { row_hf[x] -= row_mf[x]; row_mf[x] = RemoveRangeAroundZero(w0, row_mf[x]); } } } else { for (size_t y = 0; y < ysize; ++y) { float* BUTTERAUGLI_RESTRICT const row_mf = ps.mf[1].Row(y); float* BUTTERAUGLI_RESTRICT const row_hf = ps.hf[1].Row(y); for (size_t x = 0; x < xsize; ++x) { row_hf[x] -= row_mf[x]; row_mf[x] = AmplifyRangeAroundZero(w1, row_mf[x]); } } } } // Suppress red-green by intensity change in the high freq channels. static const double suppress = 2.96534974403; ps.hf[0] = SuppressXByY(xsize, ysize, ps.hf[0], ps.hf[1], suppress); for (int i = 0; i < 2; ++i) { // Divide hf into hf and uhf. ps.uhf[i] = ImageF(xsize, ysize); for (size_t y = 0; y < ysize; ++y) { float* BUTTERAUGLI_RESTRICT const row_uhf = ps.uhf[i].Row(y); float* BUTTERAUGLI_RESTRICT const row_hf = ps.hf[i].Row(y); for (size_t x = 0; x < xsize; ++x) { row_uhf[x] = row_hf[x]; } } ps.hf[i] = Blur(ps.hf[i], kSigmaUhf, border_hf); static const double kRemoveHfRange = 0.0287615200377; static const double kMaxclampHf = 78.8223237675; static const double kMaxclampUhf = 5.8907152736; static const float kMulSuppressHf = 1.10684769012; static const float kMulRegHf = 0.478741530298; static const float kRegHf = 2000 * kMulRegHf; static const float kMulSuppressUhf = 1.76905001176; static const float kMulRegUhf = 0.310148420674; static const float kRegUhf = 2000 * kMulRegUhf; if (i == 0) { for (size_t y = 0; y < ysize; ++y) { float* BUTTERAUGLI_RESTRICT const row_uhf = ps.uhf[0].Row(y); float* BUTTERAUGLI_RESTRICT const row_hf = ps.hf[0].Row(y); for (size_t x = 0; x < xsize; ++x) { row_uhf[x] -= row_hf[x]; row_hf[x] = RemoveRangeAroundZero(kRemoveHfRange, row_hf[x]); } } } else { for (size_t y = 0; y < ysize; ++y) { float* BUTTERAUGLI_RESTRICT const row_uhf = ps.uhf[1].Row(y); float* BUTTERAUGLI_RESTRICT const row_hf = ps.hf[1].Row(y); float* BUTTERAUGLI_RESTRICT const row_lf = ps.lf[1].Row(y); for (size_t x = 0; x < xsize; ++x) { row_uhf[x] -= row_hf[x]; row_hf[x] = MaximumClamp(row_hf[x], kMaxclampHf); row_uhf[x] = MaximumClamp(row_uhf[x], kMaxclampUhf); row_uhf[x] = SuppressUhfInBrightAreas(row_uhf[x], row_lf[x], kMulSuppressUhf, kRegUhf); row_hf[x] = SuppressHfInBrightAreas(row_hf[x], row_lf[x], kMulSuppressHf, kRegHf); } } } } // Modify range around zero code only concerns the high frequency // planes and only the X and Y channels. // Convert low freq xyb to vals space so that we can do a simple squared sum // diff on the low frequencies later. for (size_t y = 0; y < ysize; ++y) { float* BUTTERAUGLI_RESTRICT const row_x = ps.lf[0].Row(y); float* BUTTERAUGLI_RESTRICT const row_y = ps.lf[1].Row(y); float* BUTTERAUGLI_RESTRICT const row_b = ps.lf[2].Row(y); for (size_t x = 0; x < xsize; ++x) { float valx, valy, valb; XybLowFreqToVals(row_x[x], row_y[x], row_b[x], &valx, &valy, &valb); row_x[x] = valx; row_y[x] = valy; row_b[x] = valb; } } } static void SameNoiseLevels(const ImageF& i0, const ImageF& i1, const double kSigma, const double w, const double maxclamp, ImageF* BUTTERAUGLI_RESTRICT diffmap) { ImageF blurred(i0.xsize(), i0.ysize()); for (size_t y = 0; y < i0.ysize(); ++y) { const float* BUTTERAUGLI_RESTRICT const row0 = i0.Row(y); const float* BUTTERAUGLI_RESTRICT const row1 = i1.Row(y); float* BUTTERAUGLI_RESTRICT const to = blurred.Row(y); for (size_t x = 0; x < i0.xsize(); ++x) { double v0 = fabs(row0[x]); double v1 = fabs(row1[x]); if (v0 > maxclamp) v0 = maxclamp; if (v1 > maxclamp) v1 = maxclamp; to[x] = v0 - v1; } } blurred = Blur(blurred, kSigma, 0.0); for (size_t y = 0; y < i0.ysize(); ++y) { const float* BUTTERAUGLI_RESTRICT const row = blurred.Row(y); float* BUTTERAUGLI_RESTRICT const row_diff = diffmap->Row(y); for (size_t x = 0; x < i0.xsize(); ++x) { double diff = row[x]; row_diff[x] += w * diff * diff; } } } static void L2Diff(const ImageF& i0, const ImageF& i1, const double w, ImageF* BUTTERAUGLI_RESTRICT diffmap) { if (w == 0) { return; } for (size_t y = 0; y < i0.ysize(); ++y) { const float* BUTTERAUGLI_RESTRICT const row0 = i0.Row(y); const float* BUTTERAUGLI_RESTRICT const row1 = i1.Row(y); float* BUTTERAUGLI_RESTRICT const row_diff = diffmap->Row(y); for (size_t x = 0; x < i0.xsize(); ++x) { double diff = row0[x] - row1[x]; row_diff[x] += w * diff * diff; } } } // i0 is the original image. // i1 is the deformed copy. static void L2DiffAsymmetric(const ImageF& i0, const ImageF& i1, double w_0gt1, double w_0lt1, ImageF* BUTTERAUGLI_RESTRICT diffmap) { if (w_0gt1 == 0 && w_0lt1 == 0) { return; } w_0gt1 *= 0.8; w_0lt1 *= 0.8; for (size_t y = 0; y < i0.ysize(); ++y) { const float* BUTTERAUGLI_RESTRICT const row0 = i0.Row(y); const float* BUTTERAUGLI_RESTRICT const row1 = i1.Row(y); float* BUTTERAUGLI_RESTRICT const row_diff = diffmap->Row(y); for (size_t x = 0; x < i0.xsize(); ++x) { // Primary symmetric quadratic objective. double diff = row0[x] - row1[x]; row_diff[x] += w_0gt1 * diff * diff; // Secondary half-open quadratic objectives. const double fabs0 = fabs(row0[x]); const double too_small = 0.4 * fabs0; const double too_big = 1.0 * fabs0; if (row0[x] < 0) { if (row1[x] > -too_small) { double v = row1[x] + too_small; row_diff[x] += w_0lt1 * v * v; } else if (row1[x] < -too_big) { double v = -row1[x] - too_big; row_diff[x] += w_0lt1 * v * v; } } else { if (row1[x] < too_small) { double v = too_small - row1[x]; row_diff[x] += w_0lt1 * v * v; } else if (row1[x] > too_big) { double v = row1[x] - too_big; row_diff[x] += w_0lt1 * v * v; } } } } } // Making a cluster of local errors to be more impactful than // just a single error. ImageF CalculateDiffmap(const ImageF& diffmap_in) { PROFILER_FUNC; // Take square root. ImageF diffmap(diffmap_in.xsize(), diffmap_in.ysize()); static const float kInitialSlope = 100.0f; for (size_t y = 0; y < diffmap.ysize(); ++y) { const float* const BUTTERAUGLI_RESTRICT row_in = diffmap_in.Row(y); float* const BUTTERAUGLI_RESTRICT row_out = diffmap.Row(y); for (size_t x = 0; x < diffmap.xsize(); ++x) { const float orig_val = row_in[x]; // TODO(b/29974893): Until that is fixed do not call sqrt on very small // numbers. row_out[x] = (orig_val < (1.0f / (kInitialSlope * kInitialSlope)) ? kInitialSlope * orig_val : std::sqrt(orig_val)); } } { static const double kSigma = 1.72547472444; static const double mul1 = 0.458794906198; static const float scale = 1.0f / (1.0f + mul1); static const double border_ratio = 1.0; // 2.01209066992; ImageF blurred = Blur(diffmap, kSigma, border_ratio); for (int y = 0; y < diffmap.ysize(); ++y) { const float* const BUTTERAUGLI_RESTRICT row_blurred = blurred.Row(y); float* const BUTTERAUGLI_RESTRICT row = diffmap.Row(y); for (int x = 0; x < diffmap.xsize(); ++x) { row[x] += mul1 * row_blurred[x]; row[x] *= scale; } } } return diffmap; } void MaskPsychoImage(const PsychoImage& pi0, const PsychoImage& pi1, const size_t xsize, const size_t ysize, std::vector* BUTTERAUGLI_RESTRICT mask, std::vector* BUTTERAUGLI_RESTRICT mask_dc) { std::vector mask_xyb0 = CreatePlanes(xsize, ysize, 3); std::vector mask_xyb1 = CreatePlanes(xsize, ysize, 3); static const double muls[4] = { 0, 1.64178305129, 0.831081703362, 3.23680933546, }; for (int i = 0; i < 2; ++i) { double a = muls[2 * i]; double b = muls[2 * i + 1]; for (size_t y = 0; y < ysize; ++y) { const float* const BUTTERAUGLI_RESTRICT row_hf0 = pi0.hf[i].Row(y); const float* const BUTTERAUGLI_RESTRICT row_hf1 = pi1.hf[i].Row(y); const float* const BUTTERAUGLI_RESTRICT row_uhf0 = pi0.uhf[i].Row(y); const float* const BUTTERAUGLI_RESTRICT row_uhf1 = pi1.uhf[i].Row(y); float* const BUTTERAUGLI_RESTRICT row0 = mask_xyb0[i].Row(y); float* const BUTTERAUGLI_RESTRICT row1 = mask_xyb1[i].Row(y); for (size_t x = 0; x < xsize; ++x) { row0[x] = a * row_uhf0[x] + b * row_hf0[x]; row1[x] = a * row_uhf1[x] + b * row_hf1[x]; } } } Mask(mask_xyb0, mask_xyb1, mask, mask_dc); } ButteraugliComparator::ButteraugliComparator(const std::vector& rgb0) : xsize_(rgb0[0].xsize()), ysize_(rgb0[0].ysize()), num_pixels_(xsize_ * ysize_) { if (xsize_ < 8 || ysize_ < 8) return; std::vector xyb0 = OpsinDynamicsImage(rgb0); SeparateFrequencies(xsize_, ysize_, xyb0, pi0_); } void ButteraugliComparator::Mask( std::vector* BUTTERAUGLI_RESTRICT mask, std::vector* BUTTERAUGLI_RESTRICT mask_dc) const { MaskPsychoImage(pi0_, pi0_, xsize_, ysize_, mask, mask_dc); } void ButteraugliComparator::Diffmap(const std::vector& rgb1, ImageF &result) const { PROFILER_FUNC; if (xsize_ < 8 || ysize_ < 8) return; DiffmapOpsinDynamicsImage(OpsinDynamicsImage(rgb1), result); } void ButteraugliComparator::DiffmapOpsinDynamicsImage( const std::vector& xyb1, ImageF &result) const { PROFILER_FUNC; if (xsize_ < 8 || ysize_ < 8) return; PsychoImage pi1; SeparateFrequencies(xsize_, ysize_, xyb1, pi1); result = ImageF(xsize_, ysize_); DiffmapPsychoImage(pi1, result); } void ButteraugliComparator::DiffmapPsychoImage(const PsychoImage& pi1, ImageF& result) const { PROFILER_FUNC; const float hf_asymmetry_ = 0.8f; if (xsize_ < 8 || ysize_ < 8) { return; } std::vector block_diff_dc(3); std::vector block_diff_ac(3); for (int c = 0; c < 3; ++c) { block_diff_dc[c] = ImageF(xsize_, ysize_, 0.0); block_diff_ac[c] = ImageF(xsize_, ysize_, 0.0); } static const double wUhfMalta = 5.1409625726; static const double norm1Uhf = 58.5001247061; MaltaDiffMap(pi0_.uhf[1], pi1.uhf[1], wUhfMalta * hf_asymmetry_, wUhfMalta / hf_asymmetry_, norm1Uhf, &block_diff_ac[1]); static const double wUhfMaltaX = 4.91743441556; static const double norm1UhfX = 687196.39002; MaltaDiffMap(pi0_.uhf[0], pi1.uhf[0], wUhfMaltaX * hf_asymmetry_, wUhfMaltaX / hf_asymmetry_, norm1UhfX, &block_diff_ac[0]); static const double wHfMalta = 153.671655716; static const double norm1Hf = 83150785.9592; MaltaDiffMapLF(pi0_.hf[1], pi1.hf[1], wHfMalta * sqrt(hf_asymmetry_), wHfMalta / sqrt(hf_asymmetry_), norm1Hf, &block_diff_ac[1]); static const double wHfMaltaX = 668.358918152; static const double norm1HfX = 0.882954368025; MaltaDiffMapLF(pi0_.hf[0], pi1.hf[0], wHfMaltaX * sqrt(hf_asymmetry_), wHfMaltaX / sqrt(hf_asymmetry_), norm1HfX, &block_diff_ac[0]); static const double wMfMalta = 6841.81248144; static const double norm1Mf = 0.0135134962487; MaltaDiffMapLF(pi0_.mf[1], pi1.mf[1], wMfMalta, wMfMalta, norm1Mf, &block_diff_ac[1]); static const double wMfMaltaX = 813.901703816; static const double norm1MfX = 16792.9322251; MaltaDiffMapLF(pi0_.mf[0], pi1.mf[0], wMfMaltaX, wMfMaltaX, norm1MfX, &block_diff_ac[0]); static const double wmul[9] = { 0, 32.4449876135, 0, 0, 0, 0, 1.01370836411, 0, 1.74566011615, }; static const double maxclamp = 85.7047444518; static const double kSigmaHfX = 10.6666499623; static const double w = 884.809801415; SameNoiseLevels(pi0_.hf[1], pi1.hf[1], kSigmaHfX, w, maxclamp, &block_diff_ac[1]); for (int c = 0; c < 3; ++c) { if (c < 2) { L2DiffAsymmetric(pi0_.hf[c], pi1.hf[c], wmul[c] * hf_asymmetry_, wmul[c] / hf_asymmetry_, &block_diff_ac[c]); } L2Diff(pi0_.mf[c], pi1.mf[c], wmul[3 + c], &block_diff_ac[c]); L2Diff(pi0_.lf[c], pi1.lf[c], wmul[6 + c], &block_diff_dc[c]); } std::vector mask_xyb; std::vector mask_xyb_dc; MaskPsychoImage(pi0_, pi1, xsize_, ysize_, &mask_xyb, &mask_xyb_dc); result = CalculateDiffmap( CombineChannels(mask_xyb, mask_xyb_dc, block_diff_dc, block_diff_ac)); } // Allows PaddedMaltaUnit to call either function via overloading. struct MaltaTagLF {}; struct MaltaTag {}; static float MaltaUnit(MaltaTagLF, const float* BUTTERAUGLI_RESTRICT d, const int xs) { const int xs3 = 3 * xs; float retval = 0; { // x grows, y constant float sum = d[-4] + d[-2] + d[0] + d[2] + d[4]; retval += sum * sum; } { // y grows, x constant float sum = d[-xs3 - xs] + d[-xs - xs] + d[0] + d[xs + xs] + d[xs3 + xs]; retval += sum * sum; } { // both grow float sum = d[-xs3 - 3] + d[-xs - xs - 2] + d[0] + d[xs + xs + 2] + d[xs3 + 3]; retval += sum * sum; } { // y grows, x shrinks float sum = d[-xs3 + 3] + d[-xs - xs + 2] + d[0] + d[xs + xs - 2] + d[xs3 - 3]; retval += sum * sum; } { // y grows -4 to 4, x shrinks 1 -> -1 float sum = d[-xs3 - xs + 1] + d[-xs - xs + 1] + d[0] + d[xs + xs - 1] + d[xs3 + xs - 1]; retval += sum * sum; } { // y grows -4 to 4, x grows -1 -> 1 float sum = d[-xs3 - xs - 1] + d[-xs - xs - 1] + d[0] + d[xs + xs + 1] + d[xs3 + xs + 1]; retval += sum * sum; } { // x grows -4 to 4, y grows -1 to 1 float sum = d[-4 - xs] + d[-2 - xs] + d[0] + d[2 + xs] + d[4 + xs]; retval += sum * sum; } { // x grows -4 to 4, y shrinks 1 to -1 float sum = d[-4 + xs] + d[-2 + xs] + d[0] + d[2 - xs] + d[4 - xs]; retval += sum * sum; } { /* 0_________ 1__*______ 2___*_____ 3_________ 4____0____ 5_________ 6_____*___ 7______*__ 8_________ */ float sum = d[-xs3 - 2] + d[-xs - xs - 1] + d[0] + d[xs + xs + 1] + d[xs3 + 2]; retval += sum * sum; } { /* 0_________ 1______*__ 2_____*___ 3_________ 4____0____ 5_________ 6___*_____ 7__*______ 8_________ */ float sum = d[-xs3 + 2] + d[-xs - xs + 1] + d[0] + d[xs + xs - 1] + d[xs3 - 2]; retval += sum * sum; } { /* 0_________ 1_________ 2_*_______ 3__*______ 4____0____ 5______*__ 6_______*_ 7_________ 8_________ */ float sum = d[-xs - xs - 3] + d[-xs - 2] + d[0] + d[xs + 2] + d[xs + xs + 3]; retval += sum * sum; } { /* 0_________ 1_________ 2_______*_ 3______*__ 4____0____ 5__*______ 6_*_______ 7_________ 8_________ */ float sum = d[-xs - xs + 3] + d[-xs + 2] + d[0] + d[xs - 2] + d[xs + xs - 3]; retval += sum * sum; } { /* 0_________ 1_________ 2________* 3______*__ 4____0____ 5__*______ 6*________ 7_________ 8_________ */ float sum = d[xs + xs - 4] + d[xs - 2] + d[0] + d[-xs + 2] + d[-xs - xs + 4]; retval += sum * sum; } { /* 0_________ 1_________ 2*________ 3__*______ 4____0____ 5______*__ 6________* 7_________ 8_________ */ float sum = d[-xs - xs - 4] + d[-xs - 2] + d[0] + d[xs + 2] + d[xs + xs + 4]; retval += sum * sum; } { /* 0__*______ 1_________ 2___*_____ 3_________ 4____0____ 5_________ 6_____*___ 7_________ 8______*__ */ float sum = d[-xs3 - xs - 2] + d[-xs - xs - 1] + d[0] + d[xs + xs + 1] + d[xs3 + xs + 2]; retval += sum * sum; } { /* 0______*__ 1_________ 2_____*___ 3_________ 4____0____ 5_________ 6___*_____ 7_________ 8__*______ */ float sum = d[-xs3 - xs + 2] + d[-xs - xs + 1] + d[0] + d[xs + xs - 1] + d[xs3 + xs - 2]; retval += sum * sum; } return retval; } static float MaltaUnit(MaltaTag, const float* BUTTERAUGLI_RESTRICT d, const int xs) { const int xs3 = 3 * xs; float retval = 0; { // x grows, y constant float sum = d[-4] + d[-3] + d[-2] + d[-1] + d[0] + d[1] + d[2] + d[3] + d[4]; retval += sum * sum; } { // y grows, x constant float sum = d[-xs3 - xs] + d[-xs3] + d[-xs - xs] + d[-xs] + d[0] + d[xs] + d[xs + xs] + d[xs3] + d[xs3 + xs]; retval += sum * sum; } { // both grow float sum = d[-xs3 - 3] + d[-xs - xs - 2] + d[-xs - 1] + d[0] + d[xs + 1] + d[xs + xs + 2] + d[xs3 + 3]; retval += sum * sum; } { // y grows, x shrinks float sum = d[-xs3 + 3] + d[-xs - xs + 2] + d[-xs + 1] + d[0] + d[xs - 1] + d[xs + xs - 2] + d[xs3 - 3]; retval += sum * sum; } { // y grows -4 to 4, x shrinks 1 -> -1 float sum = d[-xs3 - xs + 1] + d[-xs3 + 1] + d[-xs - xs + 1] + d[-xs] + d[0] + d[xs] + d[xs + xs - 1] + d[xs3 - 1] + d[xs3 + xs - 1]; retval += sum * sum; } { // y grows -4 to 4, x grows -1 -> 1 float sum = d[-xs3 - xs - 1] + d[-xs3 - 1] + d[-xs - xs - 1] + d[-xs] + d[0] + d[xs] + d[xs + xs + 1] + d[xs3 + 1] + d[xs3 + xs + 1]; retval += sum * sum; } { // x grows -4 to 4, y grows -1 to 1 float sum = d[-4 - xs] + d[-3 - xs] + d[-2 - xs] + d[-1] + d[0] + d[1] + d[2 + xs] + d[3 + xs] + d[4 + xs]; retval += sum * sum; } { // x grows -4 to 4, y shrinks 1 to -1 float sum = d[-4 + xs] + d[-3 + xs] + d[-2 + xs] + d[-1] + d[0] + d[1] + d[2 - xs] + d[3 - xs] + d[4 - xs]; retval += sum * sum; } { /* 0_________ 1__*______ 2___*_____ 3___*_____ 4____0____ 5_____*___ 6_____*___ 7______*__ 8_________ */ float sum = d[-xs3 - 2] + d[-xs - xs - 1] + d[-xs - 1] + d[0] + d[xs + 1] + d[xs + xs + 1] + d[xs3 + 2]; retval += sum * sum; } { /* 0_________ 1______*__ 2_____*___ 3_____*___ 4____0____ 5___*_____ 6___*_____ 7__*______ 8_________ */ float sum = d[-xs3 + 2] + d[-xs - xs + 1] + d[-xs + 1] + d[0] + d[xs - 1] + d[xs + xs - 1] + d[xs3 - 2]; retval += sum * sum; } { /* 0_________ 1_________ 2_*_______ 3__**_____ 4____0____ 5_____**__ 6_______*_ 7_________ 8_________ */ float sum = d[-xs - xs - 3] + d[-xs - 2] + d[-xs - 1] + d[0] + d[xs + 1] + d[xs + 2] + d[xs + xs + 3]; retval += sum * sum; } { /* 0_________ 1_________ 2_______*_ 3_____**__ 4____0____ 5__**_____ 6_*_______ 7_________ 8_________ */ float sum = d[-xs - xs + 3] + d[-xs + 2] + d[-xs + 1] + d[0] + d[xs - 1] + d[xs - 2] + d[xs + xs - 3]; retval += sum * sum; } { /* 0_________ 1_________ 2_________ 3______**_ 4____0*___ 5__**_____ 6**_______ 7_________ 8_________ */ float sum = d[xs + xs - 4] + d[xs + xs - 3] + d[xs - 2] + d[xs - 1] + d[0] + d[1] + d[-xs + 2] + d[-xs + 3]; retval += sum * sum; } { /* 0_________ 1_________ 2**_______ 3__**_____ 4____0*___ 5______**_ 6_________ 7_________ 8_________ */ float sum = d[-xs - xs - 4] + d[-xs - xs - 3] + d[-xs - 2] + d[-xs - 1] + d[0] + d[1] + d[xs + 2] + d[xs + 3]; retval += sum * sum; } { /* 0__*______ 1__*______ 2___*_____ 3___*_____ 4____0____ 5____*____ 6_____*___ 7_____*___ 8_________ */ float sum = d[-xs3 - xs - 2] + d[-xs3 - 2] + d[-xs - xs - 1] + d[-xs - 1] + d[0] + d[xs] + d[xs + xs + 1] + d[xs3 + 1]; retval += sum * sum; } { /* 0______*__ 1______*__ 2_____*___ 3_____*___ 4____0____ 5____*____ 6___*_____ 7___*_____ 8_________ */ float sum = d[-xs3 - xs + 2] + d[-xs3 + 2] + d[-xs - xs + 1] + d[-xs + 1] + d[0] + d[xs] + d[xs + xs - 1] + d[xs3 - 1]; retval += sum * sum; } return retval; } // Returns MaltaUnit. "fastMode" avoids bounds-checks when x0 and y0 are known // to be far enough from the image borders. template static BUTTERAUGLI_INLINE float PaddedMaltaUnit( float* const BUTTERAUGLI_RESTRICT diffs, const size_t x0, const size_t y0, const size_t xsize_, const size_t ysize_) { int ix0 = y0 * xsize_ + x0; const float* BUTTERAUGLI_RESTRICT d = &diffs[ix0]; if (fastMode || (x0 >= 4 && y0 >= 4 && x0 < (xsize_ - 4) && y0 < (ysize_ - 4))) { return MaltaUnit(Tag(), d, xsize_); } float borderimage[9 * 9]; for (int dy = 0; dy < 9; ++dy) { int y = y0 + dy - 4; if (y < 0 || y >= ysize_) { for (int dx = 0; dx < 9; ++dx) { borderimage[dy * 9 + dx] = 0.0f; } } else { for (int dx = 0; dx < 9; ++dx) { int x = x0 + dx - 4; if (x < 0 || x >= xsize_) { borderimage[dy * 9 + dx] = 0.0f; } else { borderimage[dy * 9 + dx] = diffs[y * xsize_ + x]; } } } } return MaltaUnit(Tag(), &borderimage[4 * 9 + 4], 9); } template static void MaltaDiffMapImpl(const ImageF& lum0, const ImageF& lum1, const size_t xsize_, const size_t ysize_, const double w_0gt1, const double w_0lt1, double norm1, const double len, const double mulli, ImageF* block_diff_ac) { const float kWeight0 = 0.5; const float kWeight1 = 0.33; const double w_pre0gt1 = mulli * sqrt(kWeight0 * w_0gt1) / (len * 2 + 1); const double w_pre0lt1 = mulli * sqrt(kWeight1 * w_0lt1) / (len * 2 + 1); const float norm2_0gt1 = w_pre0gt1 * norm1; const float norm2_0lt1 = w_pre0lt1 * norm1; std::vector diffs(ysize_ * xsize_); for (size_t y = 0, ix = 0; y < ysize_; ++y) { const float* BUTTERAUGLI_RESTRICT const row0 = lum0.Row(y); const float* BUTTERAUGLI_RESTRICT const row1 = lum1.Row(y); for (size_t x = 0; x < xsize_; ++x, ++ix) { const float absval = 0.5 * std::abs(row0[x]) + 0.5 * std::abs(row1[x]); const float diff = row0[x] - row1[x]; const float scaler = norm2_0gt1 / (static_cast(norm1) + absval); // Primary symmetric quadratic objective. diffs[ix] = scaler * diff; const float scaler2 = norm2_0lt1 / (static_cast(norm1) + absval); const double fabs0 = fabs(row0[x]); // Secondary half-open quadratic objectives. const double too_small = 0.55 * fabs0; const double too_big = 1.05 * fabs0; if (row0[x] < 0) { if (row1[x] > -too_small) { double impact = scaler2 * (row1[x] + too_small); if (diff < 0) { diffs[ix] -= impact; } else { diffs[ix] += impact; } } else if (row1[x] < -too_big) { double impact = scaler2 * (-row1[x] - too_big); if (diff < 0) { diffs[ix] -= impact; } else { diffs[ix] += impact; } } } else { if (row1[x] < too_small) { double impact = scaler2 * (too_small - row1[x]); if (diff < 0) { diffs[ix] -= impact; } else { diffs[ix] += impact; } } else if (row1[x] > too_big) { double impact = scaler2 * (row1[x] - too_big); if (diff < 0) { diffs[ix] -= impact; } else { diffs[ix] += impact; } } } } } size_t y0 = 0; // Top for (; y0 < 4; ++y0) { float* const BUTTERAUGLI_RESTRICT row_diff = block_diff_ac->Row(y0); for (size_t x0 = 0; x0 < xsize_; ++x0) { row_diff[x0] += PaddedMaltaUnit(&diffs[0], x0, y0, xsize_, ysize_); } } // Middle for (; y0 < ysize_ - 4; ++y0) { float* const BUTTERAUGLI_RESTRICT row_diff = block_diff_ac->Row(y0); size_t x0 = 0; for (; x0 < 4; ++x0) { row_diff[x0] += PaddedMaltaUnit(&diffs[0], x0, y0, xsize_, ysize_); } for (; x0 < xsize_ - 4; ++x0) { row_diff[x0] += PaddedMaltaUnit(&diffs[0], x0, y0, xsize_, ysize_); } for (; x0 < xsize_; ++x0) { row_diff[x0] += PaddedMaltaUnit(&diffs[0], x0, y0, xsize_, ysize_); } } // Bottom for (; y0 < ysize_; ++y0) { float* const BUTTERAUGLI_RESTRICT row_diff = block_diff_ac->Row(y0); for (size_t x0 = 0; x0 < xsize_; ++x0) { row_diff[x0] += PaddedMaltaUnit(&diffs[0], x0, y0, xsize_, ysize_); } } } void ButteraugliComparator::MaltaDiffMap( const ImageF& lum0, const ImageF& lum1, const double w_0gt1, const double w_0lt1, const double norm1, ImageF* BUTTERAUGLI_RESTRICT block_diff_ac) const { PROFILER_FUNC; const double len = 3.75; static const double mulli = 0.354191303559; MaltaDiffMapImpl(lum0, lum1, xsize_, ysize_, w_0gt1, w_0lt1, norm1, len, mulli, block_diff_ac); } void ButteraugliComparator::MaltaDiffMapLF( const ImageF& lum0, const ImageF& lum1, const double w_0gt1, const double w_0lt1, const double norm1, ImageF* BUTTERAUGLI_RESTRICT block_diff_ac) const { PROFILER_FUNC; const double len = 3.75; static const double mulli = 0.405371989604; MaltaDiffMapImpl(lum0, lum1, xsize_, ysize_, w_0gt1, w_0lt1, norm1, len, mulli, block_diff_ac); } ImageF ButteraugliComparator::CombineChannels( const std::vector& mask_xyb, const std::vector& mask_xyb_dc, const std::vector& block_diff_dc, const std::vector& block_diff_ac) const { PROFILER_FUNC; ImageF result(xsize_, ysize_); for (size_t y = 0; y < ysize_; ++y) { float* const BUTTERAUGLI_RESTRICT row_out = result.Row(y); for (size_t x = 0; x < xsize_; ++x) { float mask[3]; float dc_mask[3]; float diff_dc[3]; float diff_ac[3]; for (int i = 0; i < 3; ++i) { mask[i] = mask_xyb[i].Row(y)[x]; dc_mask[i] = mask_xyb_dc[i].Row(y)[x]; diff_dc[i] = block_diff_dc[i].Row(y)[x]; diff_ac[i] = block_diff_ac[i].Row(y)[x]; } row_out[x] = (DotProduct(diff_dc, dc_mask) + DotProduct(diff_ac, mask)); } } return result; } double ButteraugliScoreFromDiffmap(const ImageF& diffmap) { PROFILER_FUNC; float retval = 0.0f; for (size_t y = 0; y < diffmap.ysize(); ++y) { const float * const BUTTERAUGLI_RESTRICT row = diffmap.Row(y); for (size_t x = 0; x < diffmap.xsize(); ++x) { retval = std::max(retval, row[x]); } } return retval; } #include // ===== Functions used by Mask only ===== static std::array MakeMask( double extmul, double extoff, double mul, double offset, double scaler) { std::array lut; for (int i = 0; i < lut.size(); ++i) { const double c = mul / ((0.01 * scaler * i) + offset); lut[i] = kGlobalScale * (1.0 + extmul * (c + extoff)); if (lut[i] < 1e-5) { lut[i] = 1e-5; } assert(lut[i] >= 0.0); lut[i] *= lut[i]; } return lut; } double MaskX(double delta) { static const double extmul = 2.59885507073; static const double extoff = 3.08805636789; static const double offset = 0.315424196682; static const double scaler = 16.2770141832; static const double mul = 5.62939030582; static const std::array lut = MakeMask(extmul, extoff, mul, offset, scaler); return InterpolateClampNegative(lut.data(), lut.size(), delta); } double MaskY(double delta) { static const double extmul = 0.9613705131; static const double extoff = -0.581933100068; static const double offset = 1.00846207765; static const double scaler = 2.2342321176; static const double mul = 6.64307621174; static const std::array lut = MakeMask(extmul, extoff, mul, offset, scaler); return InterpolateClampNegative(lut.data(), lut.size(), delta); } double MaskDcX(double delta) { static const double extmul = 10.0470705878; static const double extoff = 3.18472654033; static const double offset = 0.0551512255218; static const double scaler = 70.0; static const double mul = 0.373092999662; static const std::array lut = MakeMask(extmul, extoff, mul, offset, scaler); return InterpolateClampNegative(lut.data(), lut.size(), delta); } double MaskDcY(double delta) { static const double extmul = 0.0115640939227; static const double extoff = 45.9483175519; static const double offset = 0.0142290066313; static const double scaler = 5.0; static const double mul = 2.52611324247; static const std::array lut = MakeMask(extmul, extoff, mul, offset, scaler); return InterpolateClampNegative(lut.data(), lut.size(), delta); } ImageF DiffPrecompute(const ImageF& xyb0, const ImageF& xyb1) { PROFILER_FUNC; const size_t xsize = xyb0.xsize(); const size_t ysize = xyb0.ysize(); ImageF result(xsize, ysize); size_t x2, y2; for (size_t y = 0; y < ysize; ++y) { if (y + 1 < ysize) { y2 = y + 1; } else if (y > 0) { y2 = y - 1; } else { y2 = y; } const float* const BUTTERAUGLI_RESTRICT row0_in = xyb0.Row(y); const float* const BUTTERAUGLI_RESTRICT row1_in = xyb1.Row(y); const float* const BUTTERAUGLI_RESTRICT row0_in2 = xyb0.Row(y2); const float* const BUTTERAUGLI_RESTRICT row1_in2 = xyb1.Row(y2); float* const BUTTERAUGLI_RESTRICT row_out = result.Row(y); for (size_t x = 0; x < xsize; ++x) { if (x + 1 < xsize) { x2 = x + 1; } else if (x > 0) { x2 = x - 1; } else { x2 = x; } double sup0 = (fabs(row0_in[x] - row0_in[x2]) + fabs(row0_in[x] - row0_in2[x])); double sup1 = (fabs(row1_in[x] - row1_in[x2]) + fabs(row1_in[x] - row1_in2[x])); static const double mul0 = 0.918416534734; row_out[x] = mul0 * std::min(sup0, sup1); static const double cutoff = 55.0184555849; if (row_out[x] >= cutoff) { row_out[x] = cutoff; } } } return result; } void Mask(const std::vector& xyb0, const std::vector& xyb1, std::vector* BUTTERAUGLI_RESTRICT mask, std::vector* BUTTERAUGLI_RESTRICT mask_dc) { PROFILER_FUNC; const size_t xsize = xyb0[0].xsize(); const size_t ysize = xyb0[0].ysize(); mask->resize(3); *mask_dc = CreatePlanes(xsize, ysize, 3); double muls[2] = { 0.207017089891, 0.267138152891, }; double normalizer = { 1.0 / (muls[0] + muls[1]), }; static const double r0 = 2.3770330432; static const double r1 = 9.04353323561; static const double r2 = 9.24456601467; static const double border_ratio = -0.0724948220913; { // X component ImageF diff = DiffPrecompute(xyb0[0], xyb1[0]); ImageF blurred = Blur(diff, r2, border_ratio); (*mask)[0] = ImageF(xsize, ysize); for (size_t y = 0; y < ysize; ++y) { for (size_t x = 0; x < xsize; ++x) { (*mask)[0].Row(y)[x] = blurred.Row(y)[x]; } } } { // Y component (*mask)[1] = ImageF(xsize, ysize); ImageF diff = DiffPrecompute(xyb0[1], xyb1[1]); ImageF blurred1 = Blur(diff, r0, border_ratio); ImageF blurred2 = Blur(diff, r1, border_ratio); for (size_t y = 0; y < ysize; ++y) { for (size_t x = 0; x < xsize; ++x) { const double val = normalizer * ( muls[0] * blurred1.Row(y)[x] + muls[1] * blurred2.Row(y)[x]); (*mask)[1].Row(y)[x] = val; } } } // B component (*mask)[2] = ImageF(xsize, ysize); static const double mul[2] = { 16.6963293877, 2.1364621982, }; static const double w00 = 36.4671237619; static const double w11 = 2.1887170895; static const double w_ytob_hf = std::max( 0.086624184478, 0.0); static const double w_ytob_lf = 21.6804277046; static const double p1_to_p0 = 0.0513061271723; for (size_t y = 0; y < ysize; ++y) { for (size_t x = 0; x < xsize; ++x) { const double s0 = (*mask)[0].Row(y)[x]; const double s1 = (*mask)[1].Row(y)[x]; const double p1 = mul[1] * w11 * s1; const double p0 = mul[0] * w00 * s0 + p1_to_p0 * p1; (*mask)[0].Row(y)[x] = MaskX(p0); (*mask)[1].Row(y)[x] = MaskY(p1); (*mask)[2].Row(y)[x] = w_ytob_hf * MaskY(p1); (*mask_dc)[0].Row(y)[x] = MaskDcX(p0); (*mask_dc)[1].Row(y)[x] = MaskDcY(p1); (*mask_dc)[2].Row(y)[x] = w_ytob_lf * MaskDcY(p1); } } } void ButteraugliDiffmap(const std::vector &rgb0_image, const std::vector &rgb1_image, ImageF &result_image) { const size_t xsize = rgb0_image[0].xsize(); const size_t ysize = rgb0_image[0].ysize(); static const int kMax = 8; if (xsize < kMax || ysize < kMax) { // Butteraugli values for small (where xsize or ysize is smaller // than 8 pixels) images are non-sensical, but most likely it is // less disruptive to try to compute something than just give up. // Temporarily extend the borders of the image to fit 8 x 8 size. int xborder = xsize < kMax ? (kMax - xsize) / 2 : 0; int yborder = ysize < kMax ? (kMax - ysize) / 2 : 0; size_t xscaled = std::max(kMax, xsize); size_t yscaled = std::max(kMax, ysize); std::vector scaled0 = CreatePlanes(xscaled, yscaled, 3); std::vector scaled1 = CreatePlanes(xscaled, yscaled, 3); for (int i = 0; i < 3; ++i) { for (int y = 0; y < yscaled; ++y) { for (int x = 0; x < xscaled; ++x) { size_t x2 = std::min(xsize - 1, std::max(0, x - xborder)); size_t y2 = std::min(ysize - 1, std::max(0, y - yborder)); scaled0[i].Row(y)[x] = rgb0_image[i].Row(y2)[x2]; scaled1[i].Row(y)[x] = rgb1_image[i].Row(y2)[x2]; } } } ImageF diffmap_scaled; ButteraugliDiffmap(scaled0, scaled1, diffmap_scaled); result_image = ImageF(xsize, ysize); for (int y = 0; y < ysize; ++y) { for (int x = 0; x < xsize; ++x) { result_image.Row(y)[x] = diffmap_scaled.Row(y + yborder)[x + xborder]; } } return; } ButteraugliComparator butteraugli(rgb0_image); butteraugli.Diffmap(rgb1_image, result_image); } bool ButteraugliInterface(const std::vector &rgb0, const std::vector &rgb1, ImageF &diffmap, double &diffvalue) { const size_t xsize = rgb0[0].xsize(); const size_t ysize = rgb0[0].ysize(); if (xsize < 1 || ysize < 1) { return false; // No image. } for (int i = 1; i < 3; i++) { if (rgb0[i].xsize() != xsize || rgb0[i].ysize() != ysize || rgb1[i].xsize() != xsize || rgb1[i].ysize() != ysize) { return false; // Image planes must have same dimensions. } } ButteraugliDiffmap(rgb0, rgb1, diffmap); diffvalue = ButteraugliScoreFromDiffmap(diffmap); return true; } bool ButteraugliAdaptiveQuantization(size_t xsize, size_t ysize, const std::vector > &rgb, std::vector &quant) { if (xsize < 16 || ysize < 16) { return false; // Butteraugli is undefined for small images. } size_t size = xsize * ysize; std::vector rgb_planes = PlanesFromPacked(xsize, ysize, rgb); std::vector scale_xyb; std::vector scale_xyb_dc; Mask(rgb_planes, rgb_planes, &scale_xyb, &scale_xyb_dc); quant.reserve(size); // Mask gives us values in 3 color channels, but for now we take only // the intensity channel. for (size_t y = 0; y < ysize; ++y) { for (size_t x = 0; x < xsize; ++x) { quant.push_back(scale_xyb[1].Row(y)[x]); } } return true; } double ButteraugliFuzzyClass(double score) { static const double fuzzy_width_up = 6.07887388532; static const double fuzzy_width_down = 5.50793514384; static const double m0 = 2.0; static const double scaler = 0.840253347958; double val; if (score < 1.0) { // val in [scaler .. 2.0] val = m0 / (1.0 + exp((score - 1.0) * fuzzy_width_down)); val -= 1.0; // from [1 .. 2] to [0 .. 1] val *= 2.0 - scaler; // from [0 .. 1] to [0 .. 2.0 - scaler] val += scaler; // from [0 .. 2.0 - scaler] to [scaler .. 2.0] } else { // val in [0 .. scaler] val = m0 / (1.0 + exp((score - 1.0) * fuzzy_width_up)); val *= scaler; } return val; } double ButteraugliFuzzyInverse(double seek) { double pos = 0; for (double range = 1.0; range >= 1e-10; range *= 0.5) { double cur = ButteraugliFuzzyClass(pos); if (cur < seek) { pos -= range; } else { pos += range; } } return pos; } namespace { void ScoreToRgb(double score, double good_threshold, double bad_threshold, uint8_t rgb[3]) { double heatmap[12][3] = { {0, 0, 0}, {0, 0, 1}, {0, 1, 1}, {0, 1, 0}, // Good level {1, 1, 0}, {1, 0, 0}, // Bad level {1, 0, 1}, {0.5, 0.5, 1.0}, {1.0, 0.5, 0.5}, // Pastel colors for the very bad quality range. {1.0, 1.0, 0.5}, { 1, 1, 1, }, { 1, 1, 1, }, }; if (score < good_threshold) { score = (score / good_threshold) * 0.3; } else if (score < bad_threshold) { score = 0.3 + (score - good_threshold) / (bad_threshold - good_threshold) * 0.15; } else { score = 0.45 + (score - bad_threshold) / (bad_threshold * 12) * 0.5; } static const int kTableSize = sizeof(heatmap) / sizeof(heatmap[0]); score = std::min(std::max(score * (kTableSize - 1), 0.0), kTableSize - 2); int ix = static_cast(score); double mix = score - ix; for (int i = 0; i < 3; ++i) { double v = mix * heatmap[ix + 1][i] + (1 - mix) * heatmap[ix][i]; rgb[i] = static_cast(255 * pow(v, 0.5) + 0.5); } } } // namespace void CreateHeatMapImage(const std::vector& distmap, double good_threshold, double bad_threshold, size_t xsize, size_t ysize, std::vector* heatmap) { heatmap->resize(3 * xsize * ysize); for (size_t y = 0; y < ysize; ++y) { for (size_t x = 0; x < xsize; ++x) { int px = xsize * y + x; double d = distmap[px]; uint8_t* rgb = &(*heatmap)[3 * px]; ScoreToRgb(d, good_threshold, bad_threshold, rgb); } } } } // namespace butteraugli ================================================ FILE: third_party/butteraugli/butteraugli/butteraugli.h ================================================ // Copyright 2016 Google Inc. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Disclaimer: This is not an official Google product. // // Author: Jyrki Alakuijala (jyrki.alakuijala@gmail.com) #ifndef BUTTERAUGLI_BUTTERAUGLI_H_ #define BUTTERAUGLI_BUTTERAUGLI_H_ #include #include #include #include #include #include #include #include #define BUTTERAUGLI_ENABLE_CHECKS 0 // This is the main interface to butteraugli image similarity // analysis function. namespace butteraugli { template class Image; using Image8 = Image; using ImageF = Image; // ButteraugliInterface defines the public interface for butteraugli. // // It calculates the difference between rgb0 and rgb1. // // rgb0 and rgb1 contain the images. rgb0[c][px] and rgb1[c][px] contains // the red image for c == 0, green for c == 1, blue for c == 2. Location index // px is calculated as y * xsize + x. // // Value of pixels of images rgb0 and rgb1 need to be represented as raw // intensity. Most image formats store gamma corrected intensity in pixel // values. This gamma correction has to be removed, by applying the following // function: // butteraugli_val = 255.0 * pow(png_val / 255.0, gamma); // A typical value of gamma is 2.2. It is usually stored in the image header. // Take care not to confuse that value with its inverse. The gamma value should // be always greater than one. // Butteraugli does not work as intended if the caller does not perform // gamma correction. // // diffmap will contain an image of the size xsize * ysize, containing // localized differences for values px (indexed with the px the same as rgb0 // and rgb1). diffvalue will give a global score of similarity. // // A diffvalue smaller than kButteraugliGood indicates that images can be // observed as the same image. // diffvalue larger than kButteraugliBad indicates that a difference between // the images can be observed. // A diffvalue between kButteraugliGood and kButteraugliBad indicates that // a subtle difference can be observed between the images. // // Returns true on success. bool ButteraugliInterface(const std::vector &rgb0, const std::vector &rgb1, ImageF &diffmap, double &diffvalue); const double kButteraugliQuantLow = 0.26; const double kButteraugliQuantHigh = 1.454; // Converts the butteraugli score into fuzzy class values that are continuous // at the class boundary. The class boundary location is based on human // raters, but the slope is arbitrary. Particularly, it does not reflect // the expectation value of probabilities of the human raters. It is just // expected that a smoother class boundary will allow for higher-level // optimization algorithms to work faster. // // Returns 2.0 for a perfect match, and 1.0 for 'ok', 0.0 for bad. Because the // scoring is fuzzy, a butteraugli score of 0.96 would return a class of // around 1.9. double ButteraugliFuzzyClass(double score); // Input values should be in range 0 (bad) to 2 (good). Use // kButteraugliNormalization as normalization. double ButteraugliFuzzyInverse(double seek); // Returns a map which can be used for adaptive quantization. Values can // typically range from kButteraugliQuantLow to kButteraugliQuantHigh. Low // values require coarse quantization (e.g. near random noise), high values // require fine quantization (e.g. in smooth bright areas). bool ButteraugliAdaptiveQuantization(size_t xsize, size_t ysize, const std::vector > &rgb, std::vector &quant); // Implementation details, don't use anything below or your code will // break in the future. #ifdef _MSC_VER #define BUTTERAUGLI_RESTRICT __restrict #else #define BUTTERAUGLI_RESTRICT __restrict__ #endif #ifdef _MSC_VER #define BUTTERAUGLI_INLINE __forceinline #else #define BUTTERAUGLI_INLINE inline #endif #ifdef __clang__ // Early versions of Clang did not support __builtin_assume_aligned. #define BUTTERAUGLI_HAS_ASSUME_ALIGNED __has_builtin(__builtin_assume_aligned) #elif defined(__GNUC__) #define BUTTERAUGLI_HAS_ASSUME_ALIGNED 1 #else #define BUTTERAUGLI_HAS_ASSUME_ALIGNED 0 #endif // Returns a void* pointer which the compiler then assumes is N-byte aligned. // Example: float* PIK_RESTRICT aligned = (float*)PIK_ASSUME_ALIGNED(in, 32); // // The assignment semantics are required by GCC/Clang. ICC provides an in-place // __assume_aligned, whereas MSVC's __assume appears unsuitable. #if BUTTERAUGLI_HAS_ASSUME_ALIGNED #define BUTTERAUGLI_ASSUME_ALIGNED(ptr, align) __builtin_assume_aligned((ptr), (align)) #else #define BUTTERAUGLI_ASSUME_ALIGNED(ptr, align) (ptr) #endif // BUTTERAUGLI_HAS_ASSUME_ALIGNED // Functions that depend on the cache line size. class CacheAligned { public: static constexpr size_t kPointerSize = sizeof(void *); static constexpr size_t kCacheLineSize = 64; // The aligned-return annotation is only allowed on function declarations. static void *Allocate(const size_t bytes); static void Free(void *aligned_pointer); }; template using CacheAlignedUniquePtrT = std::unique_ptr; using CacheAlignedUniquePtr = CacheAlignedUniquePtrT; template static inline CacheAlignedUniquePtrT Allocate(const size_t entries) { return CacheAlignedUniquePtrT( static_cast( CacheAligned::Allocate(entries * sizeof(T))), CacheAligned::Free); } // Returns the smallest integer not less than "amount" that is divisible by // "multiple", which must be a power of two. template static inline size_t Align(const size_t amount) { static_assert(multiple != 0 && ((multiple & (multiple - 1)) == 0), "Align<> argument must be a power of two"); return (amount + multiple - 1) & ~(multiple - 1); } // Single channel, contiguous (cache-aligned) rows separated by padding. // T must be POD. // // Rationale: vectorization benefits from aligned operands - unaligned loads and // especially stores are expensive when the address crosses cache line // boundaries. Introducing padding after each row ensures the start of a row is // aligned, and that row loops can process entire vectors (writes to the padding // are allowed and ignored). // // We prefer a planar representation, where channels are stored as separate // 2D arrays, because that simplifies vectorization (repeating the same // operation on multiple adjacent components) without the complexity of a // hybrid layout (8 R, 8 G, 8 B, ...). In particular, clients can easily iterate // over all components in a row and Image requires no knowledge of the pixel // format beyond the component type "T". The downside is that we duplicate the // xsize/ysize members for each channel. // // This image layout could also be achieved with a vector and a row accessor // function, but a class wrapper with support for "deleter" allows wrapping // existing memory allocated by clients without copying the pixels. It also // provides convenient accessors for xsize/ysize, which shortens function // argument lists. Supports move-construction so it can be stored in containers. template class Image { // Returns cache-aligned row stride, being careful to avoid 2K aliasing. static size_t BytesPerRow(const size_t xsize) { // Allow reading one extra AVX-2 vector on the right margin. const size_t row_size = xsize * sizeof(T) + 32; const size_t align = CacheAligned::kCacheLineSize; size_t bytes_per_row = (row_size + align - 1) & ~(align - 1); // During the lengthy window before writes are committed to memory, CPUs // guard against read after write hazards by checking the address, but // only the lower 11 bits. We avoid a false dependency between writes to // consecutive rows by ensuring their sizes are not multiples of 2 KiB. if (bytes_per_row % 2048 == 0) { bytes_per_row += align; } return bytes_per_row; } public: using T = ComponentType; Image() : xsize_(0), ysize_(0), bytes_per_row_(0), bytes_(static_cast(nullptr), Ignore) {} Image(const size_t xsize, const size_t ysize) : xsize_(xsize), ysize_(ysize), bytes_per_row_(BytesPerRow(xsize)), bytes_(Allocate(bytes_per_row_ * ysize)) {} Image(const size_t xsize, const size_t ysize, T val) : xsize_(xsize), ysize_(ysize), bytes_per_row_(BytesPerRow(xsize)), bytes_(Allocate(bytes_per_row_ * ysize)) { for (size_t y = 0; y < ysize_; ++y) { T* const BUTTERAUGLI_RESTRICT row = Row(y); for (int x = 0; x < xsize_; ++x) { row[x] = val; } } } Image(const size_t xsize, const size_t ysize, uint8_t * const BUTTERAUGLI_RESTRICT bytes, const size_t bytes_per_row) : xsize_(xsize), ysize_(ysize), bytes_per_row_(bytes_per_row), bytes_(bytes, Ignore) {} // Move constructor (required for returning Image from function) Image(Image &&other) : xsize_(other.xsize_), ysize_(other.ysize_), bytes_per_row_(other.bytes_per_row_), bytes_(std::move(other.bytes_)) {} // Move assignment (required for std::vector) Image &operator=(Image &&other) { xsize_ = other.xsize_; ysize_ = other.ysize_; bytes_per_row_ = other.bytes_per_row_; bytes_ = std::move(other.bytes_); return *this; } void Swap(Image &other) { std::swap(xsize_, other.xsize_); std::swap(ysize_, other.ysize_); std::swap(bytes_per_row_, other.bytes_per_row_); std::swap(bytes_, other.bytes_); } // How many pixels. size_t xsize() const { return xsize_; } size_t ysize() const { return ysize_; } T *const BUTTERAUGLI_RESTRICT Row(const size_t y) { #ifdef BUTTERAUGLI_ENABLE_CHECKS if (y >= ysize_) { printf("Row %zu out of bounds (ysize=%zu)\n", y, ysize_); abort(); } #endif void *row = bytes_.get() + y * bytes_per_row_; return reinterpret_cast(BUTTERAUGLI_ASSUME_ALIGNED(row, 64)); } const T *const BUTTERAUGLI_RESTRICT Row(const size_t y) const { #ifdef BUTTERAUGLI_ENABLE_CHECKS if (y >= ysize_) { printf("Const row %zu out of bounds (ysize=%zu)\n", y, ysize_); abort(); } #endif void *row = bytes_.get() + y * bytes_per_row_; return reinterpret_cast(BUTTERAUGLI_ASSUME_ALIGNED(row, 64)); } // Raw access to byte contents, for interfacing with other libraries. // Unsigned char instead of char to avoid surprises (sign extension). uint8_t * const BUTTERAUGLI_RESTRICT bytes() { return bytes_.get(); } const uint8_t * const BUTTERAUGLI_RESTRICT bytes() const { return bytes_.get(); } size_t bytes_per_row() const { return bytes_per_row_; } // Returns number of pixels (some of which are padding) per row. Useful for // computing other rows via pointer arithmetic. intptr_t PixelsPerRow() const { static_assert(CacheAligned::kCacheLineSize % sizeof(T) == 0, "Padding must be divisible by the pixel size."); return static_cast(bytes_per_row_ / sizeof(T)); } private: // Deleter used when bytes are not owned. static void Ignore(void *ptr) {} // (Members are non-const to enable assignment during move-assignment.) size_t xsize_; // original intended pixels, not including any padding. size_t ysize_; size_t bytes_per_row_; // [bytes] including padding. CacheAlignedUniquePtr bytes_; }; // Returns newly allocated planes of the given dimensions. template static inline std::vector> CreatePlanes(const size_t xsize, const size_t ysize, const size_t num_planes) { std::vector> planes; planes.reserve(num_planes); for (size_t i = 0; i < num_planes; ++i) { planes.emplace_back(xsize, ysize); } return planes; } // Returns a new image with the same dimensions and pixel values. template static inline Image CopyPixels(const Image &other) { Image copy(other.xsize(), other.ysize()); const void *BUTTERAUGLI_RESTRICT from = other.bytes(); void *BUTTERAUGLI_RESTRICT to = copy.bytes(); memcpy(to, from, other.ysize() * other.bytes_per_row()); return copy; } // Returns new planes with the same dimensions and pixel values. template static inline std::vector> CopyPlanes( const std::vector> &planes) { std::vector> copy; copy.reserve(planes.size()); for (const Image &plane : planes) { copy.push_back(CopyPixels(plane)); } return copy; } // Compacts a padded image into a preallocated packed vector. template static inline void CopyToPacked(const Image &from, std::vector *to) { const size_t xsize = from.xsize(); const size_t ysize = from.ysize(); #if BUTTERAUGLI_ENABLE_CHECKS if (to->size() < xsize * ysize) { printf("%zu x %zu exceeds %zu capacity\n", xsize, ysize, to->size()); abort(); } #endif for (size_t y = 0; y < ysize; ++y) { const float* const BUTTERAUGLI_RESTRICT row_from = from.Row(y); float* const BUTTERAUGLI_RESTRICT row_to = to->data() + y * xsize; memcpy(row_to, row_from, xsize * sizeof(T)); } } // Expands a packed vector into a preallocated padded image. template static inline void CopyFromPacked(const std::vector &from, Image *to) { const size_t xsize = to->xsize(); const size_t ysize = to->ysize(); assert(from.size() == xsize * ysize); for (size_t y = 0; y < ysize; ++y) { const float* const BUTTERAUGLI_RESTRICT row_from = from.data() + y * xsize; float* const BUTTERAUGLI_RESTRICT row_to = to->Row(y); memcpy(row_to, row_from, xsize * sizeof(T)); } } template static inline std::vector> PlanesFromPacked( const size_t xsize, const size_t ysize, const std::vector> &packed) { std::vector> planes; planes.reserve(packed.size()); for (const std::vector &p : packed) { planes.push_back(Image(xsize, ysize)); CopyFromPacked(p, &planes.back()); } return planes; } template static inline std::vector> PackedFromPlanes( const std::vector> &planes) { assert(!planes.empty()); const size_t num_pixels = planes[0].xsize() * planes[0].ysize(); std::vector> packed; packed.reserve(planes.size()); for (const Image &image : planes) { packed.push_back(std::vector(num_pixels)); CopyToPacked(image, &packed.back()); } return packed; } struct PsychoImage { std::vector uhf; std::vector hf; std::vector mf; std::vector lf; }; class ButteraugliComparator { public: ButteraugliComparator(const std::vector& rgb0); // Computes the butteraugli map between the original image given in the // constructor and the distorted image give here. void Diffmap(const std::vector& rgb1, ImageF& result) const; // Same as above, but OpsinDynamicsImage() was already applied. void DiffmapOpsinDynamicsImage(const std::vector& xyb1, ImageF& result) const; // Same as above, but the frequency decomposition was already applied. void DiffmapPsychoImage(const PsychoImage& ps1, ImageF &result) const; void Mask(std::vector* BUTTERAUGLI_RESTRICT mask, std::vector* BUTTERAUGLI_RESTRICT mask_dc) const; private: void MaltaDiffMapLF(const ImageF& y0, const ImageF& y1, double w_0gt1, double w_0lt1, double normalization, ImageF* BUTTERAUGLI_RESTRICT block_diff_ac) const; void MaltaDiffMap(const ImageF& y0, const ImageF& y1, double w_0gt1, double w_0lt1, double normalization, ImageF* BUTTERAUGLI_RESTRICT block_diff_ac) const; ImageF CombineChannels(const std::vector& scale_xyb, const std::vector& scale_xyb_dc, const std::vector& block_diff_dc, const std::vector& block_diff_ac) const; const size_t xsize_; const size_t ysize_; const size_t num_pixels_; PsychoImage pi0_; }; void ButteraugliDiffmap(const std::vector &rgb0, const std::vector &rgb1, ImageF &diffmap); double ButteraugliScoreFromDiffmap(const ImageF& distmap); // Generate rgb-representation of the distance between two images. void CreateHeatMapImage(const std::vector &distmap, double good_threshold, double bad_threshold, size_t xsize, size_t ysize, std::vector *heatmap); // Compute values of local frequency and dc masking based on the activity // in the two images. void Mask(const std::vector& xyb0, const std::vector& xyb1, std::vector* BUTTERAUGLI_RESTRICT mask, std::vector* BUTTERAUGLI_RESTRICT mask_dc); template BUTTERAUGLI_INLINE void RgbToXyb(const V &r, const V &g, const V &b, V *BUTTERAUGLI_RESTRICT valx, V *BUTTERAUGLI_RESTRICT valy, V *BUTTERAUGLI_RESTRICT valb) { *valx = r - g; *valy = r + g; *valb = b; } template BUTTERAUGLI_INLINE void OpsinAbsorbance(const V &in0, const V &in1, const V &in2, V *BUTTERAUGLI_RESTRICT out0, V *BUTTERAUGLI_RESTRICT out1, V *BUTTERAUGLI_RESTRICT out2) { // https://en.wikipedia.org/wiki/Photopsin absorbance modeling. static const double mixi0 = 0.254462330846; static const double mixi1 = 0.488238255095; static const double mixi2 = 0.0635278003854; static const double mixi3 = 1.01681026909; static const double mixi4 = 0.195214015766; static const double mixi5 = 0.568019861857; static const double mixi6 = 0.0860755536007; static const double mixi7 = 1.1510118369; static const double mixi8 = 0.07374607900105684; static const double mixi9 = 0.06142425304154509; static const double mixi10 = 0.24416850520714256; static const double mixi11 = 1.20481945273; const V mix0(mixi0); const V mix1(mixi1); const V mix2(mixi2); const V mix3(mixi3); const V mix4(mixi4); const V mix5(mixi5); const V mix6(mixi6); const V mix7(mixi7); const V mix8(mixi8); const V mix9(mixi9); const V mix10(mixi10); const V mix11(mixi11); *out0 = mix0 * in0 + mix1 * in1 + mix2 * in2 + mix3; *out1 = mix4 * in0 + mix5 * in1 + mix6 * in2 + mix7; *out2 = mix8 * in0 + mix9 * in1 + mix10 * in2 + mix11; } std::vector OpsinDynamicsImage(const std::vector& rgb); ImageF Blur(const ImageF& in, float sigma, float border_ratio); double SimpleGamma(double v); double GammaMinArg(); double GammaMaxArg(); // Polynomial evaluation via Clenshaw's scheme (similar to Horner's). // Template enables compile-time unrolling of the recursion, but must reside // outside of a class due to the specialization. template static inline void ClenshawRecursion(const double x, const double *coefficients, double *b1, double *b2) { const double x_b1 = x * (*b1); const double t = (x_b1 + x_b1) - (*b2) + coefficients[INDEX]; *b2 = *b1; *b1 = t; ClenshawRecursion(x, coefficients, b1, b2); } // Base case template <> inline void ClenshawRecursion<0>(const double x, const double *coefficients, double *b1, double *b2) { const double x_b1 = x * (*b1); // The final iteration differs - no 2 * x_b1 here. *b1 = x_b1 - (*b2) + coefficients[0]; } // Rational polynomial := dividing two polynomial evaluations. These are easier // to find than minimax polynomials. struct RationalPolynomial { template static double EvaluatePolynomial(const double x, const double (&coefficients)[N]) { double b1 = 0.0; double b2 = 0.0; ClenshawRecursion(x, coefficients, &b1, &b2); return b1; } // Evaluates the polynomial at x (in [min_value, max_value]). inline double operator()(const double x) const { // First normalize to [0, 1]. const double x01 = (x - min_value) / (max_value - min_value); // And then to [-1, 1] domain of Chebyshev polynomials. const double xc = 2.0 * x01 - 1.0; const double yp = EvaluatePolynomial(xc, p); const double yq = EvaluatePolynomial(xc, q); if (yq == 0.0) return 0.0; return static_cast(yp / yq); } // Domain of the polynomials; they are undefined elsewhere. double min_value; double max_value; // Coefficients of T_n (Chebyshev polynomials of the first kind). // Degree 5/5 is a compromise between accuracy (0.1%) and numerical stability. double p[5 + 1]; double q[5 + 1]; }; static inline double GammaPolynomial(double value) { static const RationalPolynomial r = { 0.971783, 590.188894, { 98.7821300963361, 164.273222212631, 92.948112871376, 33.8165311212688, 6.91626704983562, 0.556380877028234 }, { 1, 1.64339473427892, 0.89392405219969, 0.298947051776379, 0.0507146002577288, 0.00226495093949756 }}; return r(value); } } // namespace butteraugli #endif // BUTTERAUGLI_BUTTERAUGLI_H_ ================================================ FILE: third_party/butteraugli/butteraugli/butteraugli_main.cc ================================================ #include #include #include #include #include "butteraugli/butteraugli.h" extern "C" { #include "png.h" #include "jpeglib.h" } namespace butteraugli { namespace { // "rgb": cleared and filled with same-sized image planes (one per channel); // either RGB, or RGBA if the PNG contains an alpha channel. bool ReadPNG(FILE* f, std::vector* rgb) { png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) { return false; } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, NULL, NULL); return false; } if (setjmp(png_jmpbuf(png_ptr)) != 0) { // Ok we are here because of the setjmp. png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return false; } rewind(f); png_init_io(png_ptr, f); // The png_transforms flags are as follows: // packing == convert 1,2,4 bit images, // strip == 16 -> 8 bits / channel, // shift == use sBIT dynamics, and // expand == palettes -> rgb, grayscale -> 8 bit images, tRNS -> alpha. const unsigned int png_transforms = PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_STRIP_16; png_read_png(png_ptr, info_ptr, png_transforms, NULL); png_bytep* row_pointers = png_get_rows(png_ptr, info_ptr); const int xsize = png_get_image_width(png_ptr, info_ptr); const int ysize = png_get_image_height(png_ptr, info_ptr); const int components = png_get_channels(png_ptr, info_ptr); *rgb = CreatePlanes(xsize, ysize, 3); switch (components) { case 1: { // GRAYSCALE for (int y = 0; y < ysize; ++y) { const uint8_t* const BUTTERAUGLI_RESTRICT row = row_pointers[y]; uint8_t* const BUTTERAUGLI_RESTRICT row0 = (*rgb)[0].Row(y); uint8_t* const BUTTERAUGLI_RESTRICT row1 = (*rgb)[1].Row(y); uint8_t* const BUTTERAUGLI_RESTRICT row2 = (*rgb)[2].Row(y); for (int x = 0; x < xsize; ++x) { const uint8_t gray = row[x]; row0[x] = row1[x] = row2[x] = gray; } } break; } case 2: { // GRAYSCALE_ALPHA rgb->push_back(Image8(xsize, ysize)); for (int y = 0; y < ysize; ++y) { const uint8_t* const BUTTERAUGLI_RESTRICT row = row_pointers[y]; uint8_t* const BUTTERAUGLI_RESTRICT row0 = (*rgb)[0].Row(y); uint8_t* const BUTTERAUGLI_RESTRICT row1 = (*rgb)[1].Row(y); uint8_t* const BUTTERAUGLI_RESTRICT row2 = (*rgb)[2].Row(y); uint8_t* const BUTTERAUGLI_RESTRICT row3 = (*rgb)[3].Row(y); for (int x = 0; x < xsize; ++x) { const uint8_t gray = row[2 * x + 0]; const uint8_t alpha = row[2 * x + 1]; row0[x] = gray; row1[x] = gray; row2[x] = gray; row3[x] = alpha; } } break; } case 3: { // RGB for (int y = 0; y < ysize; ++y) { const uint8_t* const BUTTERAUGLI_RESTRICT row = row_pointers[y]; uint8_t* const BUTTERAUGLI_RESTRICT row0 = (*rgb)[0].Row(y); uint8_t* const BUTTERAUGLI_RESTRICT row1 = (*rgb)[1].Row(y); uint8_t* const BUTTERAUGLI_RESTRICT row2 = (*rgb)[2].Row(y); for (int x = 0; x < xsize; ++x) { row0[x] = row[3 * x + 0]; row1[x] = row[3 * x + 1]; row2[x] = row[3 * x + 2]; } } break; } case 4: { // RGBA rgb->push_back(Image8(xsize, ysize)); for (int y = 0; y < ysize; ++y) { const uint8_t* const BUTTERAUGLI_RESTRICT row = row_pointers[y]; uint8_t* const BUTTERAUGLI_RESTRICT row0 = (*rgb)[0].Row(y); uint8_t* const BUTTERAUGLI_RESTRICT row1 = (*rgb)[1].Row(y); uint8_t* const BUTTERAUGLI_RESTRICT row2 = (*rgb)[2].Row(y); uint8_t* const BUTTERAUGLI_RESTRICT row3 = (*rgb)[3].Row(y); for (int x = 0; x < xsize; ++x) { row0[x] = row[4 * x + 0]; row1[x] = row[4 * x + 1]; row2[x] = row[4 * x + 2]; row3[x] = row[4 * x + 3]; } } break; } default: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return false; } png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return true; } const double* NewSrgbToLinearTable() { double* table = new double[256]; for (int i = 0; i < 256; ++i) { const double srgb = i / 255.0; table[i] = 255.0 * (srgb <= 0.04045 ? srgb / 12.92 : std::pow((srgb + 0.055) / 1.055, 2.4)); } return table; } void jpeg_catch_error(j_common_ptr cinfo) { (*cinfo->err->output_message) (cinfo); jmp_buf* jpeg_jmpbuf = (jmp_buf*) cinfo->client_data; jpeg_destroy(cinfo); longjmp(*jpeg_jmpbuf, 1); } // "rgb": cleared and filled with same-sized image planes (one per channel); // either RGB, or RGBA if the PNG contains an alpha channel. bool ReadJPEG(FILE* f, std::vector* rgb) { rewind(f); struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; cinfo.err = jpeg_std_error(&jerr); jmp_buf jpeg_jmpbuf; cinfo.client_data = &jpeg_jmpbuf; jerr.error_exit = jpeg_catch_error; if (setjmp(jpeg_jmpbuf)) { return false; } jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, f); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); int row_stride = cinfo.output_width * cinfo.output_components; JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); const size_t xsize = cinfo.output_width; const size_t ysize = cinfo.output_height; *rgb = CreatePlanes(xsize, ysize, 3); switch (cinfo.out_color_space) { case JCS_GRAYSCALE: while (cinfo.output_scanline < cinfo.output_height) { jpeg_read_scanlines(&cinfo, buffer, 1); const uint8_t* const BUTTERAUGLI_RESTRICT row = buffer[0]; uint8_t* const BUTTERAUGLI_RESTRICT row0 = (*rgb)[0].Row(cinfo.output_scanline - 1); uint8_t* const BUTTERAUGLI_RESTRICT row1 = (*rgb)[1].Row(cinfo.output_scanline - 1); uint8_t* const BUTTERAUGLI_RESTRICT row2 = (*rgb)[2].Row(cinfo.output_scanline - 1); for (int x = 0; x < xsize; x++) { const uint8_t gray = row[x]; row0[x] = row1[x] = row2[x] = gray; } } break; case JCS_RGB: while (cinfo.output_scanline < cinfo.output_height) { jpeg_read_scanlines(&cinfo, buffer, 1); const uint8_t* const BUTTERAUGLI_RESTRICT row = buffer[0]; uint8_t* const BUTTERAUGLI_RESTRICT row0 = (*rgb)[0].Row(cinfo.output_scanline - 1); uint8_t* const BUTTERAUGLI_RESTRICT row1 = (*rgb)[1].Row(cinfo.output_scanline - 1); uint8_t* const BUTTERAUGLI_RESTRICT row2 = (*rgb)[2].Row(cinfo.output_scanline - 1); for (int x = 0; x < xsize; x++) { row0[x] = row[3 * x + 0]; row1[x] = row[3 * x + 1]; row2[x] = row[3 * x + 2]; } } break; default: jpeg_destroy_decompress(&cinfo); return false; } jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); return true; } // Translate R, G, B channels from sRGB to linear space. If an alpha channel // is present, overlay the image over a black or white background. Overlaying // is done in the sRGB space; while technically incorrect, this is aligned with // many other software (web browsers, WebP near lossless). void FromSrgbToLinear(const std::vector& rgb, std::vector& linear, int background) { const size_t xsize = rgb[0].xsize(); const size_t ysize = rgb[0].ysize(); static const double* const kSrgbToLinearTable = NewSrgbToLinearTable(); if (rgb.size() == 3) { // RGB for (int c = 0; c < 3; c++) { linear.push_back(ImageF(xsize, ysize)); for (int y = 0; y < ysize; ++y) { const uint8_t* const BUTTERAUGLI_RESTRICT row_rgb = rgb[c].Row(y); float* const BUTTERAUGLI_RESTRICT row_linear = linear[c].Row(y); for (size_t x = 0; x < xsize; x++) { const int value = row_rgb[x]; row_linear[x] = kSrgbToLinearTable[value]; } } } } else { // RGBA for (int c = 0; c < 3; c++) { linear.push_back(ImageF(xsize, ysize)); for (int y = 0; y < ysize; ++y) { const uint8_t* const BUTTERAUGLI_RESTRICT row_rgb = rgb[c].Row(y); float* const BUTTERAUGLI_RESTRICT row_linear = linear[c].Row(y); const uint8_t* const BUTTERAUGLI_RESTRICT row_alpha = rgb[3].Row(y); for (size_t x = 0; x < xsize; x++) { int value; if (row_alpha[x] == 255) { value = row_rgb[x]; } else if (row_alpha[x] == 0) { value = background; } else { const int fg_weight = row_alpha[x]; const int bg_weight = 255 - fg_weight; value = (row_rgb[x] * fg_weight + background * bg_weight + 127) / 255; } row_linear[x] = kSrgbToLinearTable[value]; } } } } } std::vector ReadImageOrDie(const char* filename) { std::vector rgb; FILE* f = fopen(filename, "rb"); if (!f) { fprintf(stderr, "Cannot open %s\n", filename); exit(1); } unsigned char magic[2]; if (fread(magic, 1, 2, f) != 2) { fprintf(stderr, "Cannot read from %s\n", filename); exit(1); } if (magic[0] == 0xFF && magic[1] == 0xD8) { if (!ReadJPEG(f, &rgb)) { fprintf(stderr, "File %s is a malformed JPEG.\n", filename); exit(1); } } else { if (!ReadPNG(f, &rgb)) { fprintf(stderr, "File %s is neither a valid JPEG nor a valid PNG.\n", filename); exit(1); } } fclose(f); return rgb; } static void ScoreToRgb(double score, double good_threshold, double bad_threshold, uint8_t rgb[3]) { double heatmap[12][3] = { { 0, 0, 0 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 1, 0 }, // Good level { 1, 1, 0 }, { 1, 0, 0 }, // Bad level { 1, 0, 1 }, { 0.5, 0.5, 1.0 }, { 1.0, 0.5, 0.5 }, // Pastel colors for the very bad quality range. { 1.0, 1.0, 0.5 }, { 1, 1, 1, }, { 1, 1, 1, }, }; if (score < good_threshold) { score = (score / good_threshold) * 0.3; } else if (score < bad_threshold) { score = 0.3 + (score - good_threshold) / (bad_threshold - good_threshold) * 0.15; } else { score = 0.45 + (score - bad_threshold) / (bad_threshold * 12) * 0.5; } static const int kTableSize = sizeof(heatmap) / sizeof(heatmap[0]); score = std::min(std::max( score * (kTableSize - 1), 0.0), kTableSize - 2); int ix = static_cast(score); double mix = score - ix; for (int i = 0; i < 3; ++i) { double v = mix * heatmap[ix + 1][i] + (1 - mix) * heatmap[ix][i]; rgb[i] = static_cast(255 * pow(v, 0.5) + 0.5); } } void CreateHeatMapImage(const ImageF& distmap, double good_threshold, double bad_threshold, size_t xsize, size_t ysize, std::vector* heatmap) { heatmap->resize(3 * xsize * ysize); for (size_t y = 0; y < ysize; ++y) { for (size_t x = 0; x < xsize; ++x) { int px = xsize * y + x; double d = distmap.Row(y)[x]; uint8_t* rgb = &(*heatmap)[3 * px]; ScoreToRgb(d, good_threshold, bad_threshold, rgb); } } } // main() function, within butteraugli namespace for convenience. int Run(int argc, char* argv[]) { if (argc != 3 && argc != 4) { fprintf(stderr, "Usage: %s {image1.(png|jpg|jpeg)} {image2.(png|jpg|jpeg)} " "[heatmap.ppm]\n", argv[0]); return 1; } std::vector rgb1 = ReadImageOrDie(argv[1]); std::vector rgb2 = ReadImageOrDie(argv[2]); if (rgb1.size() != rgb2.size()) { fprintf(stderr, "Different number of channels: %lu vs %lu\n", rgb1.size(), rgb2.size()); exit(1); } for (size_t c = 0; c < rgb1.size(); ++c) { if (rgb1[c].xsize() != rgb2[c].xsize() || rgb1[c].ysize() != rgb2[c].ysize()) { fprintf( stderr, "The images are not equal in size: (%lu,%lu) vs (%lu,%lu)\n", rgb1[c].xsize(), rgb2[c].xsize(), rgb1[c].ysize(), rgb2[c].ysize()); return 1; } } // TODO: Figure out if it is a good idea to fetch the gamma from the image // instead of applying sRGB conversion. std::vector linear1, linear2; // Overlay the image over a black background. FromSrgbToLinear(rgb1, linear1, 0); FromSrgbToLinear(rgb2, linear2, 0); ImageF diff_map, diff_map_on_white; double diff_value; if (!butteraugli::ButteraugliInterface(linear1, linear2, diff_map, diff_value)) { fprintf(stderr, "Butteraugli comparison failed\n"); return 1; } ImageF* diff_map_ptr = &diff_map; if (rgb1.size() == 4 || rgb2.size() == 4) { // If the alpha channel is present, overlay the image over a white // background as well. FromSrgbToLinear(rgb1, linear1, 255); FromSrgbToLinear(rgb2, linear2, 255); double diff_value_on_white; if (!butteraugli::ButteraugliInterface(linear1, linear2, diff_map_on_white, diff_value_on_white)) { fprintf(stderr, "Butteraugli comparison failed\n"); return 1; } if (diff_value_on_white > diff_value) { diff_value = diff_value_on_white; diff_map_ptr = &diff_map_on_white; } } printf("%lf\n", diff_value); if (argc == 4) { const double good_quality = ::butteraugli::ButteraugliFuzzyInverse(1.5); const double bad_quality = ::butteraugli::ButteraugliFuzzyInverse(0.5); std::vector rgb; CreateHeatMapImage(*diff_map_ptr, good_quality, bad_quality, rgb1[0].xsize(), rgb2[0].ysize(), &rgb); FILE* const fmap = fopen(argv[3], "wb"); if (fmap == NULL) { fprintf(stderr, "Cannot open %s\n", argv[3]); perror("fopen"); return 1; } bool ok = true; if (fprintf(fmap, "P6\n%lu %lu\n255\n", rgb1[0].xsize(), rgb1[0].ysize()) < 0){ perror("fprintf"); ok = false; } if (ok && fwrite(rgb.data(), 1, rgb.size(), fmap) != rgb.size()) { perror("fwrite"); ok = false; } if (fclose(fmap) != 0) { perror("fclose"); ok = false; } if (!ok) return 1; } return 0; } } // namespace } // namespace butteraugli int main(int argc, char** argv) { return butteraugli::Run(argc, argv); } ================================================ FILE: third_party/butteraugli/jpeg.BUILD ================================================ # Description: # The Independent JPEG Group's JPEG runtime library. licenses(["notice"]) # custom notice-style license, see LICENSE cc_library( name = "jpeg", srcs = [ "cderror.h", "cdjpeg.h", "jaricom.c", "jcapimin.c", "jcapistd.c", "jcarith.c", "jccoefct.c", "jccolor.c", "jcdctmgr.c", "jchuff.c", "jcinit.c", "jcmainct.c", "jcmarker.c", "jcmaster.c", "jcomapi.c", "jconfig.h", "jcparam.c", "jcprepct.c", "jcsample.c", "jctrans.c", "jdapimin.c", "jdapistd.c", "jdarith.c", "jdatadst.c", "jdatasrc.c", "jdcoefct.c", "jdcolor.c", "jdct.h", "jddctmgr.c", "jdhuff.c", "jdinput.c", "jdmainct.c", "jdmarker.c", "jdmaster.c", "jdmerge.c", "jdpostct.c", "jdsample.c", "jdtrans.c", "jerror.c", "jfdctflt.c", "jfdctfst.c", "jfdctint.c", "jidctflt.c", "jidctfst.c", "jidctint.c", "jinclude.h", "jmemmgr.c", "jmemnobs.c", "jmemsys.h", "jmorecfg.h", "jquant1.c", "jquant2.c", "jutils.c", "jversion.h", "transupp.h", ], hdrs = [ "jerror.h", "jpegint.h", "jpeglib.h", ], includes = ["."], visibility = ["//visibility:public"], ) genrule( name = "configure", outs = ["jconfig.h"], cmd = "cat <$@\n" + "#define HAVE_PROTOTYPES 1\n" + "#define HAVE_UNSIGNED_CHAR 1\n" + "#define HAVE_UNSIGNED_SHORT 1\n" + "#define HAVE_STDDEF_H 1\n" + "#define HAVE_STDLIB_H 1\n" + "#ifdef WIN32\n" + "#define INLINE __inline\n" + "#else\n" + "#define INLINE __inline__\n" + "#endif\n" + "EOF\n", ) ================================================ FILE: third_party/butteraugli/png.BUILD ================================================ # Description: # libpng is the official PNG reference library. licenses(["notice"]) # BSD/MIT-like license cc_library( name = "png", srcs = [ "png.c", "pngerror.c", "pngget.c", "pngmem.c", "pngpread.c", "pngread.c", "pngrio.c", "pngrtran.c", "pngrutil.c", "pngset.c", "pngtrans.c", "pngwio.c", "pngwrite.c", "pngwtran.c", "pngwutil.c", ], hdrs = [ "png.h", "pngconf.h", ], includes = ["."], linkopts = ["-lm"], visibility = ["//visibility:public"], deps = ["@zlib_archive//:zlib"], ) ================================================ FILE: third_party/butteraugli/zlib.BUILD ================================================ package(default_visibility = ["//visibility:public"]) licenses(["notice"]) # BSD/MIT-like license (for zlib) cc_library( name = "zlib", srcs = [ "adler32.c", "compress.c", "crc32.c", "crc32.h", "deflate.c", "deflate.h", "gzclose.c", "gzguts.h", "gzlib.c", "gzread.c", "gzwrite.c", "infback.c", "inffast.c", "inffast.h", "inffixed.h", "inflate.c", "inflate.h", "inftrees.c", "inftrees.h", "trees.c", "trees.h", "uncompr.c", "zconf.h", "zutil.c", "zutil.h", ], hdrs = ["zlib.h"], includes = ["."], ) ================================================ FILE: tools/guetzli-compare.py ================================================ #!/usr/bin/env python from __future__ import print_function import os import glob import sys import subprocess BA_CMDLINE = './butteraugli {0} {1} {2}' GUETZLI_CMDLINE = './guetzli -quality {2} {0} {1}' OTHER_CMDLINE = sys.argv[3] def run(cmdline): print('running {}'.format(cmdline), file=sys.stderr) return subprocess.check_output(cmdline, shell=True) def size(filename): return os.stat(filename).st_size def ba_distance(orig, compressed): return float(run(BA_CMDLINE.format(orig, compressed, compressed + ".diffmap.pnm"))) def handle_png(png): other_jpeg = png + "." + sys.argv[1] + ".other.jpg" guetzli_jpeg = png + "." + sys.argv[1] + ".guetzli.jpg" run(OTHER_CMDLINE.format(png, other_jpeg)) other_distance = ba_distance(png, other_jpeg) left = 84.0 right = 110.0 while right - left > 0.05: q = (left + right) / 2 run(GUETZLI_CMDLINE.format(png, guetzli_jpeg, q)) guetzli_distance = ba_distance(png, guetzli_jpeg) if guetzli_distance < other_distance: right = q else: left = q run(GUETZLI_CMDLINE.format(png, guetzli_jpeg, right)) guetzli_distance = ba_distance(png, guetzli_jpeg) assert guetzli_distance < other_distance return (size(guetzli_jpeg), size(other_jpeg)) pngs = glob.glob(sys.argv[2]) sizes = (0, 0) for png in pngs: this_size = handle_png(png) print(png, this_size) sizes = (sizes[0] + this_size[0], sizes[1] + this_size[1]) print(sizes)