Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

dither.c 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * libpipi Pathetic image processing interface 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. * dither.c: dithering functions
  16. */
  17. #include "config.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "pipi.h"
  22. #include "pipi_internals.h"
  23. /* FIXME: this is not the right place for this... see pixels.c instead */
  24. void pipi_dither_24to16(pipi_image_t *img)
  25. {
  26. int *error, *nexterror;
  27. pipi_pixels_t *p;
  28. uint32_t *p32;
  29. int x, y;
  30. error = malloc(sizeof(int) * 3 * (img->w + 2));
  31. nexterror = malloc(sizeof(int) * 3 * (img->w + 2));
  32. p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_U8);
  33. p32 = (uint32_t *)p->pixels;
  34. memset(error, 0, sizeof(int) * 3 * (img->w + 2));
  35. for(y = 0; y < img->h; y++)
  36. {
  37. int er = 0, eg = 0, eb = 0;
  38. memset(nexterror, 0, sizeof(int) * 3 * (img->w + 2));
  39. for(x = 0; x < img->w; x++)
  40. {
  41. int r, g, b, a, r2, g2, b2;
  42. r = p32[y * img->w + x] & 0xff;
  43. g = (p32[y * img->w + x] >> 8) & 0xff;
  44. b = (p32[y * img->w + x] >> 16) & 0xff;
  45. a = (p32[y * img->w + x] >> 24) & 0xff;
  46. r += er + error[x * 3 + 3];
  47. g += eg + error[x * 3 + 4];
  48. b += eb + error[x * 3 + 5];
  49. r2 = r / 8 * 8; g2 = g / 4 * 4; b2 = b / 8 * 8;
  50. if(r2 < 0) r2 = 0; if(r2 > 0xf8) r2 = 0xf8;
  51. if(g2 < 0) g2 = 0; if(g2 > 0xfc) g2 = 0xfc;
  52. if(b2 < 0) b2 = 0; if(b2 > 0xf8) b2 = 0xf8;
  53. /* hack */
  54. if(r2 == 0x88 && g2 == 0x88 && b2 == 0x88) g2 = 0x84;
  55. /* hack */
  56. p32[y * img->w + x] = (a << 24) | (b2 << 16) | (g2 << 8) | r2;
  57. er = r - (r2 / 8 * 255 / 31);
  58. eg = g - (g2 / 4 * 255 / 63);
  59. eb = b - (b2 / 8 * 255 / 31);
  60. nexterror[x * 3 + 0] += er * 3 / 8;
  61. nexterror[x * 3 + 1] += eg * 3 / 8;
  62. nexterror[x * 3 + 2] += eb * 3 / 8;
  63. nexterror[x * 3 + 3] += er * 5 / 8;
  64. nexterror[x * 3 + 4] += eg * 5 / 8;
  65. nexterror[x * 3 + 5] += eb * 5 / 8;
  66. nexterror[x * 3 + 6] += er * 1 / 8;
  67. nexterror[x * 3 + 7] += eg * 1 / 8;
  68. nexterror[x * 3 + 8] += eb * 1 / 8;
  69. er -= er * 3 / 8 + er * 7 / 8 + er * 1 / 8;
  70. eg -= eg * 3 / 8 + eg * 7 / 8 + eg * 1 / 8;
  71. eb -= eb * 3 / 8 + eb * 7 / 8 + eb * 1 / 8;
  72. }
  73. memcpy(error, nexterror, sizeof(int) * 3 * (img->w + 2));
  74. }
  75. pipi_release_pixels(img, p);
  76. free(error);
  77. free(nexterror);
  78. }