Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

minmax.c 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * minmax.c: min and max computation functions
  16. */
  17. #include "config.h"
  18. #include <stdlib.h>
  19. #include "pipi.h"
  20. #include "pipi_internals.h"
  21. pipi_image_t *pipi_min(pipi_image_t *img1, pipi_image_t *img2)
  22. {
  23. pipi_image_t *dst;
  24. pipi_pixels_t *img1p, *img2p, *dstp;
  25. float *img1data, *img2data, *dstdata;
  26. int x, y, w, h;
  27. if(img1->w != img2->w || img1->h != img2->h)
  28. return NULL;
  29. w = img1->w;
  30. h = img1->h;
  31. dst = pipi_new(w, h);
  32. dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  33. dstdata = (float *)dstp->pixels;
  34. img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
  35. img1data = (float *)img1p->pixels;
  36. img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
  37. img2data = (float *)img2p->pixels;
  38. for(y = 0; y < h; y++)
  39. {
  40. for(x = 0; x < w; x++)
  41. {
  42. float p, q;
  43. p = img1data[4 * (y * w + x)];
  44. q = img2data[4 * (y * w + x)];
  45. dstdata[4 * (y * w + x)] = p < q ? p : q;
  46. p = img1data[4 * (y * w + x) + 1];
  47. q = img2data[4 * (y * w + x) + 1];
  48. dstdata[4 * (y * w + x) + 1] = p < q ? p : q;
  49. p = img1data[4 * (y * w + x) + 2];
  50. q = img2data[4 * (y * w + x) + 2];
  51. dstdata[4 * (y * w + x) + 2] = p < q ? p : q;
  52. p = img1data[4 * (y * w + x) + 3];
  53. q = img2data[4 * (y * w + x) + 3];
  54. dstdata[4 * (y * w + x) + 3] = p < q ? p : q;
  55. }
  56. }
  57. return dst;
  58. }
  59. pipi_image_t *pipi_max(pipi_image_t *img1, pipi_image_t *img2)
  60. {
  61. pipi_image_t *dst;
  62. pipi_pixels_t *img1p, *img2p, *dstp;
  63. float *img1data, *img2data, *dstdata;
  64. int x, y, w, h;
  65. if(img1->w != img2->w || img1->h != img2->h)
  66. return NULL;
  67. w = img1->w;
  68. h = img1->h;
  69. dst = pipi_new(w, h);
  70. dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  71. dstdata = (float *)dstp->pixels;
  72. img1p = pipi_getpixels(img1, PIPI_PIXELS_RGBA_F);
  73. img1data = (float *)img1p->pixels;
  74. img2p = pipi_getpixels(img2, PIPI_PIXELS_RGBA_F);
  75. img2data = (float *)img2p->pixels;
  76. for(y = 0; y < h; y++)
  77. {
  78. for(x = 0; x < w; x++)
  79. {
  80. float p, q;
  81. p = img1data[4 * (y * w + x)];
  82. q = img2data[4 * (y * w + x)];
  83. dstdata[4 * (y * w + x)] = p > q ? p : q;
  84. p = img1data[4 * (y * w + x) + 1];
  85. q = img2data[4 * (y * w + x) + 1];
  86. dstdata[4 * (y * w + x) + 1] = p > q ? p : q;
  87. p = img1data[4 * (y * w + x) + 2];
  88. q = img2data[4 * (y * w + x) + 2];
  89. dstdata[4 * (y * w + x) + 2] = p > q ? p : q;
  90. p = img1data[4 * (y * w + x) + 3];
  91. q = img2data[4 * (y * w + x) + 3];
  92. dstdata[4 * (y * w + x) + 3] = p > q ? p : q;
  93. }
  94. }
  95. return dst;
  96. }