source: libpipi/trunk/pipi/dither.c @ 2263

Last change on this file since 2263 was 2263, checked in by Sam Hocevar, 12 years ago
  • Implemented 24 -> 16 bpp dithering.
  • Added the --bpp flag to genethumb.
File size: 2.7 KB
Line 
1/*
2 *  libpipi       Proper image processing implementation library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * dither.c: dithering functions
17 */
18
19#include "config.h"
20#include "common.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "pipi.h"
27#include "pipi_internals.h"
28
29void pipi_dither_24to16(pipi_image_t *img)
30{
31    int *error, *nexterror;
32    uint32_t *p32;
33    int x, y;
34
35    error = malloc(sizeof(int) * 3 * (img->width + 2));
36    nexterror = malloc(sizeof(int) * 3 * (img->width + 2));
37    p32 = (uint32_t *)img->pixels;
38
39    memset(error, 0, sizeof(int) * 3 * (img->width + 2));
40
41    for(y = 0; y < img->height; y++)
42    {
43        int er = 0, eg = 0, eb = 0;
44
45        memset(nexterror, 0, sizeof(int) * 3 * (img->width + 2));
46
47        for(x = 0; x < img->width; x++)
48        {
49            int r, g, b, r2, g2, b2;
50            r = p32[y * img->width + x] & 0xff;
51            g = (p32[y * img->width + x] >> 8) & 0xff;
52            b = (p32[y * img->width + x] >> 16) & 0xff;
53            r += er + error[x * 3 + 3];
54            g += eg + error[x * 3 + 4];
55            b += eb + error[x * 3 + 5];
56            r2 = r / 8 * 8; g2 = g / 4 * 4; b2 = b / 8 * 8;
57            if(r2 < 0) r2 = 0; if(r2 > 0xf8) r2 = 0xf8;
58            if(g2 < 0) g2 = 0; if(g2 > 0xfc) g2 = 0xfc;
59            if(b2 < 0) b2 = 0; if(b2 > 0xf8) b2 = 0xf8;
60            /* hack */
61            if(r2 == 0x88 && g2 == 0x88 && b2 == 0x88) g2 = 0x84;
62            /* hack */
63            p32[y * img->width + x] = (b2 << 16) | (g2 << 8)  | r2;
64
65            er = r - (r2 / 8 * 255 / 31);
66            eg = g - (g2 / 4 * 255 / 63);
67            eb = b - (b2 / 8 * 255 / 31);
68            nexterror[x * 3 + 0] += er * 3 / 8;
69            nexterror[x * 3 + 1] += eg * 3 / 8;
70            nexterror[x * 3 + 2] += eb * 3 / 8;
71            nexterror[x * 3 + 3] += er * 5 / 8;
72            nexterror[x * 3 + 4] += eg * 5 / 8;
73            nexterror[x * 3 + 5] += eb * 5 / 8;
74            nexterror[x * 3 + 6] += er * 1 / 8;
75            nexterror[x * 3 + 7] += eg * 1 / 8;
76            nexterror[x * 3 + 8] += eb * 1 / 8;
77            er -= er * 3 / 8 + er * 7 / 8 + er * 1 / 8;
78            eg -= eg * 3 / 8 + eg * 7 / 8 + eg * 1 / 8;
79            eb -= eb * 3 / 8 + eb * 7 / 8 + eb * 1 / 8;
80        }
81
82        memcpy(error, nexterror, sizeof(int) * 3 * (img->width + 2));
83    }
84
85    free(error);
86    free(nexterror);
87}
88
Note: See TracBrowser for help on using the repository browser.