Browse Source

* Implemented 24 -> 16 bpp dithering.

* Added the --bpp flag to genethumb.


git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@2263 92316355-f0b4-4df1-b90c-862c8a59935f
remotes/tiles
sam 17 years ago
parent
commit
1d6c2c507d
4 changed files with 104 additions and 2 deletions
  1. +13
    -2
      genethumb/genethumb.c
  2. +1
    -0
      pipi/Makefile.am
  3. +88
    -0
      pipi/dither.c
  4. +2
    -0
      pipi/pipi.h

+ 13
- 2
genethumb/genethumb.c View File

@@ -27,7 +27,7 @@ int main(int argc, char *argv[])
char *srcname = NULL, *dstname = NULL;
pipi_image_t *src, *dst;

int i, w = 0, h = 0;
int i, w = 0, h = 0, bpp = 24;

for(;;)
{
@@ -35,14 +35,23 @@ int main(int argc, char *argv[])
static struct myoption long_options[] =
{
{ "geometry", 1, NULL, 'g' },
{ "bpp", 1, NULL, 'b' },
};
int c = mygetopt(argc, argv, "g:", long_options, &option_index);
int c = mygetopt(argc, argv, "g:b:", long_options, &option_index);

if(c == -1)
break;

switch(c)
{
case 'b':
bpp = atoi(myoptarg);
if(bpp != 32 && bpp != 24 && bpp != 16)
{
fprintf(stderr, "%s: invalid bpp -- %s\n", argv[0], myoptarg);
return EXIT_FAILURE;
}
break;
case 'g':
w = atoi(myoptarg);
if(strchr(myoptarg, 'x'))
@@ -65,6 +74,8 @@ int main(int argc, char *argv[])

src = pipi_load(srcname);
dst = pipi_resize(src, w, h);
if(bpp == 16)
pipi_dither_24to16(dst);
pipi_save(dst, dstname);
pipi_free(src);
pipi_free(dst);


+ 1
- 0
pipi/Makefile.am View File

@@ -24,6 +24,7 @@ libpipi_la_SOURCES = \
pixels.c \
codec.c \
resize.c \
dither.c \
test.c \
$(codec_sources) \
$(NULL)


+ 88
- 0
pipi/dither.c View File

@@ -0,0 +1,88 @@
/*
* libpipi Proper image processing implementation library
* Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* $Id$
*
* This library is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

/*
* dither.c: dithering functions
*/

#include "config.h"
#include "common.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pipi.h"
#include "pipi_internals.h"

void pipi_dither_24to16(pipi_image_t *img)
{
int *error, *nexterror;
uint32_t *p32;
int x, y;

error = malloc(sizeof(int) * 3 * (img->width + 2));
nexterror = malloc(sizeof(int) * 3 * (img->width + 2));
p32 = (uint32_t *)img->pixels;

memset(error, 0, sizeof(int) * 3 * (img->width + 2));

for(y = 0; y < img->height; y++)
{
int er = 0, eg = 0, eb = 0;

memset(nexterror, 0, sizeof(int) * 3 * (img->width + 2));

for(x = 0; x < img->width; x++)
{
int r, g, b, r2, g2, b2;
r = p32[y * img->width + x] & 0xff;
g = (p32[y * img->width + x] >> 8) & 0xff;
b = (p32[y * img->width + x] >> 16) & 0xff;
r += er + error[x * 3 + 3];
g += eg + error[x * 3 + 4];
b += eb + error[x * 3 + 5];
r2 = r / 8 * 8; g2 = g / 4 * 4; b2 = b / 8 * 8;
if(r2 < 0) r2 = 0; if(r2 > 0xf8) r2 = 0xf8;
if(g2 < 0) g2 = 0; if(g2 > 0xfc) g2 = 0xfc;
if(b2 < 0) b2 = 0; if(b2 > 0xf8) b2 = 0xf8;
/* hack */
if(r2 == 0x88 && g2 == 0x88 && b2 == 0x88) g2 = 0x84;
/* hack */
p32[y * img->width + x] = (b2 << 16) | (g2 << 8) | r2;

er = r - (r2 / 8 * 255 / 31);
eg = g - (g2 / 4 * 255 / 63);
eb = b - (b2 / 8 * 255 / 31);
nexterror[x * 3 + 0] += er * 3 / 8;
nexterror[x * 3 + 1] += eg * 3 / 8;
nexterror[x * 3 + 2] += eb * 3 / 8;
nexterror[x * 3 + 3] += er * 5 / 8;
nexterror[x * 3 + 4] += eg * 5 / 8;
nexterror[x * 3 + 5] += eb * 5 / 8;
nexterror[x * 3 + 6] += er * 1 / 8;
nexterror[x * 3 + 7] += eg * 1 / 8;
nexterror[x * 3 + 8] += eb * 1 / 8;
er -= er * 3 / 8 + er * 7 / 8 + er * 1 / 8;
eg -= eg * 3 / 8 + eg * 7 / 8 + eg * 1 / 8;
eb -= eb * 3 / 8 + eb * 7 / 8 + eb * 1 / 8;
}

memcpy(error, nexterror, sizeof(int) * 3 * (img->width + 2));
}

free(error);
free(nexterror);
}


+ 2
- 0
pipi/pipi.h View File

@@ -40,6 +40,8 @@ extern int pipi_setpixel(pipi_image_t *img, int x, int y,

extern pipi_image_t *pipi_resize(pipi_image_t const *, int, int);

extern void pipi_dither_24to16(pipi_image_t *img);

extern void pipi_test(pipi_image_t *);

#ifdef __cplusplus


Loading…
Cancel
Save