123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #include "ImageScrambler.h"
- #include <qdebug.h>
-
-
- // Given an image and two rectangles defined by the x,y locations of their
- // upperleft pixels and their height, width, swaps the pixels in those rectangles.
- // For example, for an image I with height and width of 100. The following invocation
- // swaps the top and bottom halves of the image:
- // cropSwap(I, 0, 0, 0, 50, 100, 50)
-
- void ImageScrambler::cropSwap(QImage &img, int x0, int y0, int x1, int y1, int width, int height ) {
- QRgb p;
-
- // For each of the pixels of the square with starting coord (x0,y0)
- // Por cada pixel del cuadrado con coordenada inicial (x0,y0)
- for (int dx = 0; dx < width; dx++) {
- for (int dy = 0; dy < height; dy++) {
- // extract the pixel
- // extrae el pixel
- p = img.pixel(x0 + dx , y0 + dy);
- // swap the pixel with the square starting coord (x1,y1)
- // intercabia el pixel con el cuadrado con coodenada inicial (x1,y1)
- img.setPixel(x0 + dx , y0 + dy, img.pixel(x1 + dx , y1 + dy));
- img.setPixel(x1 + dx , y1 + dy, p);
- }
- }
- }
-
- // This is the function you will code. Given the image, the level of recursion,
- // the x, y, width and height of the topleft corner of the rectangle that you'd
- // like to scramble, this function scrambles that part of the image.
- // For example, for an image of height and width 100, to scramble the whole image
- // two levels, you would invoke:
- // ScrambleFilter(I, 2, 0, 0, 100, 100);
-
- QImage ImageScrambler::ScrambleFilter(QImage image, int level, int sx, int sy, int width, int height){
-
- // Compute half the width and half the height to obtain the sub images
- // Computa la mitad del ancho y la mitad de la altura para obtener la sub imagen.
- int w = width / 2;
- int h = height / 2;
-
- // Condition to stop the recursion
- // Condicion para parar la recursion
- if ( level > 0 ) {
-
- if ( level % 2 ) {
-
- // Assume an image with the following quadrants:
- // 1 2
- // 3 4
- // When N is odd, we will divide the image in
- // four quadrants and swap quadrants 1 <-> 4, and 3 <-> 2.
- // 1 2 => 4 3
- // 3 4 2 1
- //
- // Asume una imagen con los siguentes cuadrantes:
- // 1 2
- // 3 4
- // Cuando N es impar, divide la imagen en
- // cuatro cuadrantes e intercambia los cuadrantes 1 <-> 4, and 3 <-> 2.
- // 1 2 => 4 3
- // 3 4 2 1
- cropSwap(image, sx, sy, sx + w, sy + h, w, h);
- cropSwap(image, sx + w , sy, sx , sy + h, w, h);
- }
- else
- // Assume an image with the following quadrants:
- // 1 2
- // When N is even, we will divide the image in
- // two quadrants and swap quadrants 1 <-> 2.
- // 1 2 => 2 1
- // Note that in this case the given width is half the width
- // and the full height is given to the cropSwap function.
- //
- // Asume una imagen con los siguientes cuadrantes:
- // 1 2
- // Cuando N es par, dividiremos la imagen en
- // dos cuadrantes e intercambiar cuadrantes 1 <-> 2.
- // 1 2 => 2 1
- // Note que en este caso el ancho dado es la mitad del ancho
- // y la altura dada es la altura completa en la funcion cropSwap.
- cropSwap(image, sx, sy, sx + w, sy, w, height);
-
- // Call the function recursively dividing the imagen on 4 quadrants
- // Invoca la funcion recursivamente dividiendo la imagen en 4 cuadrantes
-
- image = ScrambleFilter(image, level-1, sx, sy , w, h); //upper left / arriba izq
- image = ScrambleFilter(image, level-1, sx + w, sy , w, h); //upper right / arriba derecha
- image = ScrambleFilter(image, level-1, sx , sy + h, w, h); //lower left / abajo izq
- image = ScrambleFilter(image, level-1, sx + w, sy + h, w, h); //lower right / abajo derecha
- }
- return image;
- }
-
|