Blend.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * The Python Imaging Library
  3. * $Id$
  4. *
  5. * interpolate between two existing images
  6. *
  7. * history:
  8. * 96-03-20 fl Created
  9. * 96-05-18 fl Simplified blend expression
  10. * 96-10-05 fl Fixed expression bug, special case for interpolation
  11. *
  12. * Copyright (c) Fredrik Lundh 1996.
  13. * Copyright (c) Secret Labs AB 1997.
  14. *
  15. * See the README file for details on usage and redistribution.
  16. */
  17. #include "Imaging.h"
  18. Imaging
  19. ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha)
  20. {
  21. Imaging imOut;
  22. int x, y;
  23. /* Check arguments */
  24. if (!imIn1 || !imIn2 || imIn1->type != IMAGING_TYPE_UINT8
  25. || imIn1->palette || strcmp(imIn1->mode, "1") == 0
  26. || imIn2->palette || strcmp(imIn2->mode, "1") == 0)
  27. return ImagingError_ModeError();
  28. if (imIn1->type != imIn2->type ||
  29. imIn1->bands != imIn2->bands ||
  30. imIn1->xsize != imIn2->xsize ||
  31. imIn1->ysize != imIn2->ysize)
  32. return ImagingError_Mismatch();
  33. /* Shortcuts */
  34. if (alpha == 0.0)
  35. return ImagingCopy(imIn1);
  36. else if (alpha == 1.0)
  37. return ImagingCopy(imIn2);
  38. imOut = ImagingNewDirty(imIn1->mode, imIn1->xsize, imIn1->ysize);
  39. if (!imOut)
  40. return NULL;
  41. if (alpha >= 0 && alpha <= 1.0) {
  42. /* Interpolate between bands */
  43. for (y = 0; y < imIn1->ysize; y++) {
  44. UINT8* in1 = (UINT8*) imIn1->image[y];
  45. UINT8* in2 = (UINT8*) imIn2->image[y];
  46. UINT8* out = (UINT8*) imOut->image[y];
  47. for (x = 0; x < imIn1->linesize; x++)
  48. out[x] = (UINT8)
  49. ((int) in1[x] + alpha * ((int) in2[x] - (int) in1[x]));
  50. }
  51. } else {
  52. /* Extrapolation; must make sure to clip resulting values */
  53. for (y = 0; y < imIn1->ysize; y++) {
  54. UINT8* in1 = (UINT8*) imIn1->image[y];
  55. UINT8* in2 = (UINT8*) imIn2->image[y];
  56. UINT8* out = (UINT8*) imOut->image[y];
  57. for (x = 0; x < imIn1->linesize; x++) {
  58. float temp = (float)
  59. ((int) in1[x] + alpha * ((int) in2[x] - (int) in1[x]));
  60. if (temp <= 0.0)
  61. out[x] = 0;
  62. else if (temp >= 255.0)
  63. out[x] = 255;
  64. else
  65. out[x] = (UINT8) temp;
  66. }
  67. }
  68. }
  69. return imOut;
  70. }