about summary refs log tree commit diff
path: root/lib/libppmfloyd.c
blob: a7117c8ecf7f5f7bef3ace43c0ae21e13025d5da (plain) (blame)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
These functions were taken from Ingo Wilken's ilbm package by Bryan
Henderson on 01.03.10.  Because ppmtoilbm and ilbmtoppm are the only
programs that will use these in the foreseeable future, they remain
lightly documented and tested.

But they look like they would be useful in other Netpbm programs that
do Floyd-Steinberg.
*/



/* libfloyd.c - generic Floyd-Steinberg error distribution routines for PBMPlus
**
** Copyright (C) 1994 Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include "netpbm/mallocvar.h"
#include "ppm.h"
#include "ppmfloyd.h"



static void
fs_adjust(ppm_fs_info * const fi,
          int           const col) {

    int     const errcol = col+1;
    pixel * const pP     = &(fi->pixrow[col]);
    pixval  const maxval = fi->maxval;

    long r, g, b;

    /* Use Floyd-Steinberg errors to adjust actual color. */
    r = fi->thisrederr  [errcol]; if( r < 0 ) r -= 8; else r += 8; r /= 16;
    g = fi->thisgreenerr[errcol]; if( g < 0 ) g -= 8; else g += 8; g /= 16;
    b = fi->thisblueerr [errcol]; if( b < 0 ) b -= 8; else b += 8; b /= 16;

    r += PPM_GETR(*pP); if ( r < 0 ) r = 0; else if ( r > maxval ) r = maxval;
    g += PPM_GETG(*pP); if ( g < 0 ) g = 0; else if ( g > maxval ) g = maxval;
    b += PPM_GETB(*pP); if ( b < 0 ) b = 0; else if ( b > maxval ) b = maxval;

    PPM_ASSIGN(*pP, r, g, b);
    fi->red = r; fi->green = g; fi->blue = b;
}



static ppm_fs_info *
allocateFi(int const cols) {

    ppm_fs_info * fi;

    MALLOCVAR(fi);

    if (fi != NULL) {
        MALLOCARRAY(fi->thisrederr  , cols + 2);
        MALLOCARRAY(fi->thisgreenerr, cols + 2);
        MALLOCARRAY(fi->thisblueerr , cols + 2);
        MALLOCARRAY(fi->nextrederr  , cols + 2);
        MALLOCARRAY(fi->nextgreenerr, cols + 2);
        MALLOCARRAY(fi->nextblueerr , cols + 2);

        if (fi->thisrederr   == NULL ||
            fi->thisgreenerr == NULL ||
            fi->thisblueerr  == NULL ||
            fi->nextrederr   == NULL ||
            fi->nextgreenerr == NULL ||
            fi->nextblueerr  == NULL)
            pm_error("out of memory allocating "
                     "Floyd-Steinberg control structure");
    } else
        pm_error("out of memory allocating Floyd-Steinberg control structure");

    return(fi);
}



ppm_fs_info *
ppm_fs_init(unsigned int const cols,
            pixval       const maxval,
            unsigned int const flags) {

    ppm_fs_info * fiP;

    fiP = allocateFi(cols);

    fiP->lefttoright = 1;
    fiP->cols        = cols;
    fiP->maxval      = maxval;
    fiP->flags       = flags;

    if (flags & FS_RANDOMINIT) {
        unsigned int i;
        srand(pm_randseed());
        for (i = 0; i < cols +2; ++i) {
            /* random errors in [-1..+1] */
            fiP->thisrederr[i]   = rand() % 32 - 16;
            fiP->thisgreenerr[i] = rand() % 32 - 16;
            fiP->thisblueerr[i]  = rand() % 32 - 16;
        }
    } else {
        unsigned int i;

        for (i = 0; i < cols + 2; ++i)
            fiP->thisrederr[i] = fiP->thisgreenerr[i] =
                fiP->thisblueerr[i] = 0;
    }
    return fiP;
}



void
ppm_fs_free(ppm_fs_info * const fiP) {

    if (fiP) {
        free(fiP->thisrederr); free(fiP->thisgreenerr); free(fiP->thisblueerr);
        free(fiP->nextrederr); free(fiP->nextgreenerr); free(fiP->nextblueerr);
        free(fiP);
    }
}



int
ppm_fs_startrow(ppm_fs_info * const fiP,
                pixel *       const pixrow) {

    int retval;

    if (!fiP)
        retval = 0;
    else {
        unsigned int col;

        fiP->pixrow = pixrow;

        for (col = 0; col < fiP->cols + 2; ++col) {
            fiP->nextrederr  [col] = 0;
            fiP->nextgreenerr[col] = 0;
            fiP->nextblueerr [col] = 0;
        }

        if(fiP->lefttoright) {
            fiP->col_end = fiP->cols;
            col = 0;
        } else {
            fiP->col_end = -1;
            col = fiP->cols - 1;
        }
        fs_adjust(fiP, col);

        retval = col;
    }
    return retval;
}



int
ppm_fs_next(ppm_fs_info * const fiP,
            int           const startCol) {

    int col;

    col = startCol;  /* initial value */

    if (!fiP)
        ++col;
    else {
        if (fiP->lefttoright)
            ++col;
        else
            --col;
        if (col == fiP->col_end)
            col = fiP->cols;
        else
            fs_adjust(fiP, col);
    }
    return col;
}



void
ppm_fs_endrow(ppm_fs_info * const fiP) {

    if (fiP) {
        {
            long * const tmp = fiP->thisrederr;
            fiP->thisrederr = fiP->nextrederr;
            fiP->nextrederr = tmp;
        }
        {
            long * const tmp = fiP->thisgreenerr;
            fiP->thisgreenerr = fiP->nextgreenerr;
            fiP->nextgreenerr = tmp;
        }
        {
            long * const tmp = fiP->thisblueerr;
            fiP->thisblueerr = fiP->nextblueerr;
            fiP->nextblueerr = tmp;
        }
        if (fiP->flags & FS_ALTERNATE)
            fiP->lefttoright = !fiP->lefttoright;
    }
}



void
ppm_fs_update(ppm_fs_info * const fiP,
              int           const col,
              pixel *       const pP) {

    if (fiP)
        ppm_fs_update3(fiP, col, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP));
}



void
ppm_fs_update3(ppm_fs_info * const fiP,
               int           const col,
               pixval        const r,
               pixval        const g,
               pixval        const b) {

    int const errcol = col + 1;

    if (fiP) {
        long const rerr = (long)(fiP->red)   - (long)r;
        long const gerr = (long)(fiP->green) - (long)g;
        long const berr = (long)(fiP->blue)  - (long)b;

        if ( fiP->lefttoright ) {
            {
                long const two_err = 2*rerr;

                long err;

                err = rerr;     fiP->nextrederr[errcol+1] += err;    /* 1/16 */
                err += two_err; fiP->nextrederr[errcol-1] += err;    /* 3/16 */
                err += two_err; fiP->nextrederr[errcol  ] += err;    /* 5/16 */
                err += two_err; fiP->thisrederr[errcol+1] += err;    /* 7/16 */
            }
            {
                long const two_err = 2*gerr;

                long err;

                err = gerr;     fiP->nextgreenerr[errcol+1] += err;  /* 1/16 */
                err += two_err; fiP->nextgreenerr[errcol-1] += err;  /* 3/16 */
                err += two_err; fiP->nextgreenerr[errcol  ] += err;  /* 5/16 */
                err += two_err; fiP->thisgreenerr[errcol+1] += err;  /* 7/16 */
            }
            {
                long const two_err = 2*berr;

                long err;

                err = berr;     fiP->nextblueerr[errcol+1] += err;  /* 1/16 */
                err += two_err; fiP->nextblueerr[errcol-1] += err;  /* 3/16 */
                err += two_err; fiP->nextblueerr[errcol  ] += err;  /* 5/16 */
                err += two_err; fiP->thisblueerr[errcol+1] += err;  /* 7/16 */
            }
        } else {
            {
                long const two_err = 2*rerr;

                long err;

                err = rerr;     fiP->nextrederr[errcol-1] += err;    /* 1/16 */
                err += two_err; fiP->nextrederr[errcol+1] += err;    /* 3/16 */
                err += two_err; fiP->nextrederr[errcol  ] += err;    /* 5/16 */
                err += two_err; fiP->thisrederr[errcol-1] += err;    /* 7/16 */
            }
            {
                long const two_err = 2*gerr;

                long err;

                err = gerr;     fiP->nextgreenerr[errcol-1] += err;  /* 1/16 */
                err += two_err; fiP->nextgreenerr[errcol+1] += err;  /* 3/16 */
                err += two_err; fiP->nextgreenerr[errcol  ] += err;  /* 5/16 */
                err += two_err; fiP->thisgreenerr[errcol-1] += err;  /* 7/16 */
            }
            {
                long const two_err = 2*berr;

                long err;

                err = berr;     fiP->nextblueerr[errcol-1] += err;  /* 1/16 */
                err += two_err; fiP->nextblueerr[errcol+1] += err;  /* 3/16 */
                err += two_err; fiP->nextblueerr[errcol  ] += err;  /* 5/16 */
                err += two_err; fiP->thisblueerr[errcol-1] += err;  /* 7/16 */
            }
        }
    }
}