No Description

Filter.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "ImageScrambler.h"
  2. #include <qdebug.h>
  3. // Given an image and two rectangles defined by the x,y locations of their
  4. // upperleft pixels and their height, width, swaps the pixels in those rectangles.
  5. // For example, for an image I with height and width of 100. The following invocation
  6. // swaps the top and bottom halves of the image:
  7. // cropSwap(I, 0, 0, 0, 50, 100, 50)
  8. void ImageScrambler::cropSwap(QImage &img, int x0, int y0, int x1, int y1, int width, int height ) {
  9. QRgb p;
  10. // For each of the pixels of the square with starting coord (x0,y0)
  11. // Por cada pixel del cuadrado con coordenada inicial (x0,y0)
  12. for (int dx = 0; dx < width; dx++) {
  13. for (int dy = 0; dy < height; dy++) {
  14. // extract the pixel
  15. // extrae el pixel
  16. p = img.pixel(x0 + dx , y0 + dy);
  17. // swap the pixel with the square starting coord (x1,y1)
  18. // intercabia el pixel con el cuadrado con coodenada inicial (x1,y1)
  19. img.setPixel(x0 + dx , y0 + dy, img.pixel(x1 + dx , y1 + dy));
  20. img.setPixel(x1 + dx , y1 + dy, p);
  21. }
  22. }
  23. }
  24. // This is the function you will code. Given the image, the level of recursion,
  25. // the x, y, width and height of the topleft corner of the rectangle that you'd
  26. // like to scramble, this function scrambles that part of the image.
  27. // For example, for an image of height and width 100, to scramble the whole image
  28. // two levels, you would invoke:
  29. // ScrambleFilter(I, 2, 0, 0, 100, 100);
  30. QImage ImageScrambler::ScrambleFilter(QImage image, int level, int sx, int sy, int width, int height){
  31. // Compute half the width and half the height to obtain the sub images
  32. // Computa la mitad del ancho y la mitad de la altura para obtener la sub imagen.
  33. int w = width / 2;
  34. int h = height / 2;
  35. // Condition to stop the recursion
  36. // Condicion para parar la recursion
  37. if ( level > 0 ) {
  38. if ( level % 2 ) {
  39. // Assume an image with the following quadrants:
  40. // 1 2
  41. // 3 4
  42. // When N is odd, we will divide the image in
  43. // four quadrants and swap quadrants 1 <-> 4, and 3 <-> 2.
  44. // 1 2 => 4 3
  45. // 3 4 2 1
  46. //
  47. // Asume una imagen con los siguentes cuadrantes:
  48. // 1 2
  49. // 3 4
  50. // Cuando N es impar, divide la imagen en
  51. // cuatro cuadrantes e intercambia los cuadrantes 1 <-> 4, and 3 <-> 2.
  52. // 1 2 => 4 3
  53. // 3 4 2 1
  54. cropSwap(image, sx, sy, sx + w, sy + h, w, h);
  55. cropSwap(image, sx + w , sy, sx , sy + h, w, h);
  56. }
  57. else
  58. // Assume an image with the following quadrants:
  59. // 1 2
  60. // When N is even, we will divide the image in
  61. // two quadrants and swap quadrants 1 <-> 2.
  62. // 1 2 => 2 1
  63. // Note that in this case the given width is half the width
  64. // and the full height is given to the cropSwap function.
  65. //
  66. // Asume una imagen con los siguientes cuadrantes:
  67. // 1 2
  68. // Cuando N es par, dividiremos la imagen en
  69. // dos cuadrantes e intercambiar cuadrantes 1 <-> 2.
  70. // 1 2 => 2 1
  71. // Note que en este caso el ancho dado es la mitad del ancho
  72. // y la altura dada es la altura completa en la funcion cropSwap.
  73. cropSwap(image, sx, sy, sx + w, sy, w, height);
  74. // Call the function recursively dividing the imagen on 4 quadrants
  75. // Invoca la funcion recursivamente dividiendo la imagen en 4 cuadrantes
  76. image = ScrambleFilter(image, level-1, sx, sy , w, h); //upper left / arriba izq
  77. image = ScrambleFilter(image, level-1, sx + w, sy , w, h); //upper right / arriba derecha
  78. image = ScrambleFilter(image, level-1, sx , sy + h, w, h); //lower left / abajo izq
  79. image = ScrambleFilter(image, level-1, sx + w, sy + h, w, h); //lower right / abajo derecha
  80. }
  81. return image;
  82. }