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: merge.c 3411 2009-03-24 00:49:00Z sam $ |
---|

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 | * mean.c: Mean computation function |
---|

17 | */ |
---|

18 | |
---|

19 | #include "config.h" |
---|

20 | |
---|

21 | #include <stdlib.h> |
---|

22 | |
---|

23 | #include "pipi.h" |
---|

24 | #include "pipi_internals.h" |
---|

25 | |
---|

26 | pipi_image_t *pipi_merge(pipi_image_t *img1, pipi_image_t *img2, double t) |
---|

27 | { |
---|

28 | pipi_image_t *dst; |
---|

29 | pipi_pixels_t *img1p, *img2p, *dstp; |
---|

30 | float *img1data, *img2data, *dstdata; |
---|

31 | double t2; |
---|

32 | int x, y, w, h; |
---|

33 | |
---|

34 | if(img1->w != img2->w || img1->h != img2->h) |
---|

35 | return NULL; |
---|

36 | |
---|

37 | if(t < 0.0) |
---|

38 | t = 0.0; |
---|

39 | else if(t > 1.0) |
---|

40 | t = 1.0; |
---|

41 | t2 = 1.0 - t; |
---|

42 | |
---|

43 | w = img1->w; |
---|

44 | h = img1->h; |
---|

45 | |
---|

46 | dst = pipi_new(w, h); |
---|

47 | dstp = pipi_get_pixels(dst, PIPI_PIXELS_RGBA_F32); |
---|

48 | dstdata = (float *)dstp->pixels; |
---|

49 | |
---|

50 | img1p = pipi_get_pixels(img1, PIPI_PIXELS_RGBA_F32); |
---|

51 | img1data = (float *)img1p->pixels; |
---|

52 | img2p = pipi_get_pixels(img2, PIPI_PIXELS_RGBA_F32); |
---|

53 | img2data = (float *)img2p->pixels; |
---|

54 | |
---|

55 | for(y = 0; y < h; y++) |
---|

56 | { |
---|

57 | for(x = 0; x < w; x++) |
---|

58 | { |
---|

59 | float p, q; |
---|

60 | |
---|

61 | p = img1data[4 * (y * w + x)]; |
---|

62 | q = img2data[4 * (y * w + x)]; |
---|

63 | dstdata[4 * (y * w + x)] = t * p + t2 * q; |
---|

64 | |
---|

65 | p = img1data[4 * (y * w + x) + 1]; |
---|

66 | q = img2data[4 * (y * w + x) + 1]; |
---|

67 | dstdata[4 * (y * w + x) + 1] = t * p + t2 * q; |
---|

68 | |
---|

69 | p = img1data[4 * (y * w + x) + 2]; |
---|

70 | q = img2data[4 * (y * w + x) + 2]; |
---|

71 | dstdata[4 * (y * w + x) + 2] = t * p + t2 * q; |
---|

72 | |
---|

73 | p = img1data[4 * (y * w + x) + 3]; |
---|

74 | q = img2data[4 * (y * w + x) + 3]; |
---|

75 | dstdata[4 * (y * w + x) + 3] = t * p + t2 * q; |
---|

76 | } |
---|

77 | } |
---|

78 | |
---|

79 | return dst; |
---|

80 | } |
---|

81 | |
---|

82 | pipi_image_t *pipi_mean(pipi_image_t *img1, pipi_image_t *img2) |
---|

83 | { |
---|

84 | return pipi_merge(img1, img2, 0.5); |
---|

85 | } |
---|

86 | |
---|