File: PngQuant.java

package info (click to toggle)
libimagequant 4.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 532 kB
  • sloc: ansic: 430; java: 158; cs: 132; xml: 87; makefile: 72
file content (111 lines) | stat: -rw-r--r-- 3,310 bytes parent folder | download | duplicates (4)
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
package org.pngquant;

import org.pngquant.*;
import java.awt.image.*;

/**
 * Starting point for the library. Holds configuration. Equivalent of liq_attr* in libimagequant.
 */
public class PngQuant extends LiqObject {

    /**
     * Single instance can be "recycled" for many remappings.
     */
    public PngQuant() {
        handle = liq_attr_create();
    }

    public PngQuant(PngQuant other) {
        handle = liq_attr_copy(other.handle);
    }

    /**
     * 1-shot quantization and remapping with current settings.
     * @see quantize()
     *
     * @return 8-bit indexed image or null on failure
     */
    public BufferedImage getRemapped(BufferedImage bufimg) {
        try {
            Image liqimg = new Image(this, bufimg);
            BufferedImage remapped = getRemapped(liqimg);
            liqimg.close();
            return remapped;
        } catch(PngQuantException e) {
            return null;
        }
    }

    /** @return remapped image or null on failure */
    public BufferedImage getRemapped(Image liqimg) {
        Result result = quantize(liqimg);
        if (result == null) return null;
        BufferedImage remapped = result.getRemapped(liqimg);
        result.close();
        return remapped;
    }

    /**
     * Performs quantization (chooses optimal palette for the given Image).
     * Returned object can be used to customize remapping and reused to remap other images to the same palette.
     * @link http://pngquant.org/lib/#liq_quantize_image
     *
     * @return null on failure
     */
    public Result quantize(Image img) {
        try {
            return new Result(this, img);
        } catch(PngQuantException e) {
            return null;
        }
    }

    /**
     * Remapped images won't use more than given number of colors (may use less if setQuality() is used)
     *
     * @link http://pngquant.org/lib/#liq_set_max_colors
     */
    public native boolean setMaxColors(int colors);

    /**
     * Equivalent of setQuality(target/2, target)
     *
     * @link http://pngquant.org/lib/#liq_set_quality
     */
    public native boolean setQuality(int target);

    /**
     * Quality in range 0-100. Quantization will fail if minimum quality cannot
     * be achieved with given number of colors.
     *
     * @link http://pngquant.org/lib/#liq_set_quality
     */
    public native boolean setQuality(int min, int max);

    /**
     * Speed in range 1 (slowest) and 11 (fastest). 3 is the optimum.
     * Higher speeds quantize quicker, but at cost of quality and sometimes larger images.
     *
     * @link http://pngquant.org/lib/#liq_set_speed
     */
    public native boolean setSpeed(int speed);

    /**
     * Reduces color precision by truncating number of least significant bits.
     * Slightly improves speed and helps generating images for low-fidelity displays/textures.
     *
     * @link http://pngquant.org/lib/#liq_set_min_posterization
     */
    public native boolean setMinPosterization(int bits);

    public void close() {
        if (handle != 0) {
            liq_attr_destroy(handle);
            handle = 0;
        }
    }

    private static native long liq_attr_create();
    private static native long liq_attr_copy(long orig);
    private static native void liq_attr_destroy(long handle);
}