Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

92 linhas
2.8 KiB

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