Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

About

Audits and compresses Android image assets. Not another Convert-to-WebP: it decides which route each asset can safely take, and refuses the ones that would band.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Image Squeeze

An IntelliJ / Android Studio plugin that audits and compresses Android image assets.

It is not another "Convert to WebP". Android Studio already ships one. The value here is the decision logic it lacks — figuring out which route each image should take, and refusing to compress the ones that would break.

状态:单文件流程与全工程扫描均已可用,verifyPlugin 干净通过。 完整方案与分阶段计划见 docs/PLAN.md。


Why

Compressing Android assets naively goes wrong in ways that are easy to miss:

Trap What happens
Lossy alpha on a gradient alpha channel Visible banding. And any alpha_quality high enough to avoid it saves nothing.
Judging quality by per-pixel ΔRGB ΔRGB stays ~0.5 while the image is visibly striped. It cannot see structural distortion.
Measuring "noise" without compositing A PNG's transparent region holds undefined RGB garbage. Measured 7.37 vs 0.47 on the same file.
Downscaling a textured image Mean ΔRGB looks fine (diluted by flat areas); the max tells the truth.
Compressing everything under assets/ Beauty-filter LUTs are PNGs too. Compressing them changes the filter output.
Writing WebP bytes into a .png filename AAPT treats it as PNG.
Baking a background color into a shared asset Looks right in one layout, off-color in the other eleven.

Every one of these is encoded as a check, a threshold, or a regression test in core.


How it decides

The preferred route is chosen by the shape of the alpha channel, then constrained by whether the host background is actually safe to bake:

semi-transparent pixels (alpha 1..249) <= 2%   ->  KEEP_ALPHA
    lossy RGB + lossless alpha.
    A binary alpha channel cannot band, so this needs no knowledge of the host
    background and the asset stays reusable. Measured 4~21x.

otherwise (gradient alpha)
    one known solid host color                  ->  BAKE_BACKGROUND
        Composite onto the host background and drop alpha. Measured 8~24x.

    unknown/dynamic host + worthwhile saving    ->  KEEP_ALPHA fallback
        Keep alpha lossless and compress RGB. This is smaller less often, but
        remains reusable and avoids baking a wrong background into the asset.

Three independent metrics, each catching what the others miss:

  • noiseKind() — dither vs. real texture, via how much a σ=1 blur changes the picture
  • banding() — level counts / step heights along the gradient; catches quantization stripes
  • deltaRgb() — per-pixel error; catches color shifts, blind to banding

Build

Requires JDK 17.

./gradlew :core:test            # algorithms + regression tests
./gradlew :plugin:runIde        # launch a sandbox IDE with the plugin
./gradlew :plugin:buildPlugin   # -> plugin/build/distributions/*.zip

Calibrate the dither/texture thresholds against your own assets:

./gradlew :core:test --tests "*ThresholdProbe*" --rerun-tasks \
  -Dsqueeze.sampleRoot="/path/to/android-project/app/src/main/res"

Layout

core/     pure JVM, no IDE dependency, unit-testable
  ImageStats.kt              metrics + every threshold with its measured basis
  WebpCodec.kt               cwebp wrapper, three-tier binary lookup
  Analyzer.kt                route decision -> ImageReport
  HostBackgroundResolver.kt  walks layout XML parent chains for the host color
plugin/   IDE integration
  ComparePanel.kt            side-by-side preview with switchable background
  ApplyService.kt            write-back / rename via WriteCommandAction

ComparePanel renders both sides on the same, user-selectable background. This is not a nicety: the IDE's built-in image diff composites transparency over the dark theme, which makes an original look richer than a background-baked version of itself. That illusion caused two false "you broke it" calls during development.


cwebp

There is no pure-Java WebP encoder, so encoding shells out to Google's cwebp. The binaries are not committed — plugin/cwebp.gradle.kts downloads the official libwebp releases at build time and verifies each SHA-256, then processResources packs them into the plugin jar under bin/<platform>/. At runtime CwebpProvider extracts the one for the current platform (and sets the executable bit, which jars do not preserve).

Lookup order: explicit path in settings -> bundled binary -> cwebp on PATH.

Usage

  • One asset: right-click an image in the Project view -> Squeeze: Inspect This Image. Shows the original and the compressed result side by side, on the same background, and applies only if the result passes the banding check.
  • Whole project: the Image Squeeze tool window scans in parallel, ranks by bytes saved, and applies to a multi-selection in one go.

The Apply button is deliberately disabled when the route is NONE (not worth it) or when the compressed result bands. A gradient-alpha image with no unambiguous host background first falls back to KEEP_ALPHA; it is disabled only when that safe route also saves too little. Batch apply enforces the same rules and reports what it skipped and why.

Not implemented yet

Result caching between scans, and resolving host backgrounds through PSI so that theme attributes (?attr/colorSurface) also work — see docs/PLAN.md §5.

License

MIT for this repository.

The bundled cwebp binaries are BSD-3-Clause (libwebp, Google). That license requires binary redistributions to reproduce the copyright notice, and the official binary archives do not contain one — so the license text is vendored from the upstream source tree and packed into the plugin jar. See THIRD-PARTY-NOTICES.md.

About

Audits and compresses Android image assets. Not another Convert-to-WebP: it decides which route each asset can safely take, and refuses the ones that would band.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages