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

Last change on this file since 2709 was 2605, checked in by Sam Hocevar, 15 years ago
  • Big API reorganisation. Now libpipi can transparently convert between colour spaces for a given image. For instance, if pipi_gaussian_blur is applied to a 32-bpp image, it is automatically converted to gamma-corrected 32-bit floats beforehands, then converted back to normal.
  • TODO: clipping, regions of interest, more formats, getpixel macros...
File size: 2.8 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/* XXX: disabled because this is not the right place... see pixels.c instead */
32#if 0
33    int *error, *nexterror;
34    uint32_t *p32;
35    int x, y;
36
37    error = malloc(sizeof(int) * 3 * (img->width + 2));
38    nexterror = malloc(sizeof(int) * 3 * (img->width + 2));
39    p32 = (uint32_t *)img->pixels;
40
41    memset(error, 0, sizeof(int) * 3 * (img->width + 2));
42
43    for(y = 0; y < img->height; y++)
44    {
45        int er = 0, eg = 0, eb = 0;
46
47        memset(nexterror, 0, sizeof(int) * 3 * (img->width + 2));
48
49        for(x = 0; x < img->width; x++)
50        {
51            int r, g, b, r2, g2, b2;
52            r = p32[y * img->width + x] & 0xff;
53            g = (p32[y * img->width + x] >> 8) & 0xff;
54            b = (p32[y * img->width + x] >> 16) & 0xff;
55            r += er + error[x * 3 + 3];
56            g += eg + error[x * 3 + 4];
57            b += eb + error[x * 3 + 5];
58            r2 = r / 8 * 8; g2 = g / 4 * 4; b2 = b / 8 * 8;
59            if(r2 < 0) r2 = 0; if(r2 > 0xf8) r2 = 0xf8;
60            if(g2 < 0) g2 = 0; if(g2 > 0xfc) g2 = 0xfc;
61            if(b2 < 0) b2 = 0; if(b2 > 0xf8) b2 = 0xf8;
62            /* hack */
63            if(r2 == 0x88 && g2 == 0x88 && b2 == 0x88) g2 = 0x84;
64            /* hack */
65            p32[y * img->width + x] = (b2 << 16) | (g2 << 8)  | r2;
66
67            er = r - (r2 / 8 * 255 / 31);
68            eg = g - (g2 / 4 * 255 / 63);
69            eb = b - (b2 / 8 * 255 / 31);
70            nexterror[x * 3 + 0] += er * 3 / 8;
71            nexterror[x * 3 + 1] += eg * 3 / 8;
72            nexterror[x * 3 + 2] += eb * 3 / 8;
73            nexterror[x * 3 + 3] += er * 5 / 8;
74            nexterror[x * 3 + 4] += eg * 5 / 8;
75            nexterror[x * 3 + 5] += eb * 5 / 8;
76            nexterror[x * 3 + 6] += er * 1 / 8;
77            nexterror[x * 3 + 7] += eg * 1 / 8;
78            nexterror[x * 3 + 8] += eb * 1 / 8;
79            er -= er * 3 / 8 + er * 7 / 8 + er * 1 / 8;
80            eg -= eg * 3 / 8 + eg * 7 / 8 + eg * 1 / 8;
81            eb -= eb * 3 / 8 + eb * 7 / 8 + eb * 1 / 8;
82        }
83
84        memcpy(error, nexterror, sizeof(int) * 3 * (img->width + 2));
85    }
86
87    free(error);
88    free(nexterror);
89#endif
90}
91
Note: See TracBrowser for help on using the repository browser.