summaryrefslogtreecommitdiff
path: root/Year_2/IandM
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-02-18 18:37:48 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-02-18 18:37:48 +0100
commitab0dfb9eb5c0e1d39bb2e87bb3692171585a6716 (patch)
tree408002a87075f9091dff1fe98f157dff30a787d5 /Year_2/IandM
parenta68d04a87c78ed9120e05e66ed1952e1ba5d6248 (diff)
i&m: add scaling function
Diffstat (limited to 'Year_2/IandM')
-rw-r--r--Year_2/IandM/scaling/data/lena.pngbin0 -> 473831 bytes
-rw-r--r--Year_2/IandM/scaling/scaling.pde88
2 files changed, 88 insertions, 0 deletions
diff --git a/Year_2/IandM/scaling/data/lena.png b/Year_2/IandM/scaling/data/lena.png
new file mode 100644
index 0000000..59ef68a
--- /dev/null
+++ b/Year_2/IandM/scaling/data/lena.png
Binary files differ
diff --git a/Year_2/IandM/scaling/scaling.pde b/Year_2/IandM/scaling/scaling.pde
new file mode 100644
index 0000000..1597173
--- /dev/null
+++ b/Year_2/IandM/scaling/scaling.pde
@@ -0,0 +1,88 @@
+float scalex = 1.0, scaley = 1.0;
+
+void setup() {
+ size(768, 256);
+}
+
+void draw() {
+ PImage img = loadImage("lena.png");
+ img.resize(256, 256);
+
+ image(img, 0, 0);
+ image(scale_forward(img, scalex, scaley), 256, 0);
+ image(inverse_scale(img, scalex, scaley), 512, 0);
+}
+
+void keyPressed() {
+ if (key == '+') {
+ scalex+=0.1;
+ scaley+=0.1;
+ } else if (key == '-' && (scalex > 0 && scaley > 0)) {
+ scalex-=0.1;
+ scaley-=0.1;
+ }
+}
+
+PImage scale_forward(PImage I, float sx, float sy) {
+ PImage out = createImage(I.width, I.height, RGB);
+
+ out.loadPixels();
+
+ for (int i = 0; i < out.pixels.length; ++i) {
+ out.pixels[i] = color(0);
+ }
+
+ float x, y;
+ float u1, v1;
+
+ for (int u = 0; u < I.width; ++u) {
+ for (int v = 0; v < I.height; ++v) {
+ u1 = u-I.width/2;
+ v1 = v-I.height/2;
+
+ x = u1*sx;
+ y = v1*sy;
+
+ x+=I.width/2;
+ y+=I.height/2;
+
+ out.set(round(x), round(y), I.get(u, v));
+ }
+ }
+
+ out.updatePixels();
+
+ return out;
+}
+
+PImage inverse_scale(PImage I, float sx, float sy) {
+ PImage out = createImage(I.width, I.height, RGB);
+
+ out.loadPixels();
+
+ for (int i = 0; i < out.pixels.length; ++i) {
+ out.pixels[i] = color(0);
+ }
+
+ float u, v;
+ float x1, y1;
+
+ for (int x = 0; x < I.width; ++x) {
+ for (int y = 0; y < I.height; ++y) {
+ x1 = x-I.width/2;
+ y1 = y-I.height/2;
+
+ u = x1*1/sx;
+ v = y1*1/sy;
+
+ u+=I.width/2;
+ v+=I.height/2;
+
+ out.set(x, y, I.get(round(u), round(v)));
+ }
+ }
+
+ out.updatePixels();
+
+ return out;
+}