summaryrefslogtreecommitdiffstats
path: root/src/include/libplacebo/dither.h
blob: 84f17c78ad6b2ecdaa807d182a36f665928b8611 (plain)
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
/*
 * This file is part of libplacebo.
 *
 * libplacebo is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libplacebo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with libplacebo.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LIBPLACEBO_DITHER_H_
#define LIBPLACEBO_DITHER_H_

#include <libplacebo/common.h>

PL_API_BEGIN

// Generates a deterministic NxN bayer (ordered) dither matrix, storing the
// result in `data`. `size` must be a power of two. The resulting matrix will
// be roughly uniformly distributed within the range [0,1).
PL_API void pl_generate_bayer_matrix(float *data, int size);

// Generates a random NxN blue noise texture. storing the result in `data`.
// `size` must be a positive power of two no larger than 256. The resulting
// texture will be roughly uniformly distributed within the range [0,1).
//
// Note: This function is very, *very* slow for large sizes. Generating a
// dither matrix with size 256 can take several seconds on a modern processor.
PL_API void pl_generate_blue_noise(float *data, int size);

// Defines the border of all error diffusion kernels
#define PL_EDF_MIN_DX (-2)
#define PL_EDF_MAX_DX  (2)
#define PL_EDF_MAX_DY  (2)

struct pl_error_diffusion_kernel {
    const char *name; // Short and concise identifier
    const char *description; // Longer / friendly name

    // The minimum value such that a (y, x) -> (y, x + y * shift) mapping will
    // make all error pushing operations affect next column (and after it)
    // only.
    //
    // Higher shift values are significantly more computationally intensive.
    int shift;

    // The diffusion factor for (y, x) is pattern[y][x - PL_EDF_MIN_DX] / divisor.
    int pattern[PL_EDF_MAX_DY + 1][PL_EDF_MAX_DX - PL_EDF_MIN_DX + 1];
    int divisor;
};

// Algorithms with shift=1:
PL_API extern const struct pl_error_diffusion_kernel pl_error_diffusion_simple;
PL_API extern const struct pl_error_diffusion_kernel pl_error_diffusion_false_fs;
// Algorithms with shift=2:
PL_API extern const struct pl_error_diffusion_kernel pl_error_diffusion_sierra_lite;
PL_API extern const struct pl_error_diffusion_kernel pl_error_diffusion_floyd_steinberg;
PL_API extern const struct pl_error_diffusion_kernel pl_error_diffusion_atkinson;
// Algorithms with shift=3, probably too heavy for low end GPUs:
PL_API extern const struct pl_error_diffusion_kernel pl_error_diffusion_jarvis_judice_ninke;
PL_API extern const struct pl_error_diffusion_kernel pl_error_diffusion_stucki;
PL_API extern const struct pl_error_diffusion_kernel pl_error_diffusion_burkes;
PL_API extern const struct pl_error_diffusion_kernel pl_error_diffusion_sierra2;
PL_API extern const struct pl_error_diffusion_kernel pl_error_diffusion_sierra3;

// A list of built-in error diffusion kernels, terminated by NULL
PL_API extern const struct pl_error_diffusion_kernel * const pl_error_diffusion_kernels[];
PL_API extern const int pl_num_error_diffusion_kernels; // excluding trailing NULL

// Find the error diffusion kernel with the given name, or NULL on failure.
PL_API const struct pl_error_diffusion_kernel *pl_find_error_diffusion_kernel(const char *name);

PL_API_END

#endif // LIBPLACEBO_DITHER_H_